1 /*
2  * Copyright (C) 2020 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 #ifndef ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
17 #define ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
18 
19 #include <android/hardware/sensors/1.0/types.h>
20 #include <poll.h>
21 #include <condition_variable>
22 #include <memory>
23 #include <mutex>
24 #include <thread>
25 #include <vector>
26 #include "iio_utils.h"
27 
28 #define NUM_OF_CHANNEL_SUPPORTED 4
29 
30 using ::android::hardware::sensors::V1_0::Event;
31 using ::android::hardware::sensors::V1_0::OperationMode;
32 using ::android::hardware::sensors::V1_0::Result;
33 using ::android::hardware::sensors::V1_0::SensorInfo;
34 using ::android::hardware::sensors::V1_0::SensorType;
35 
36 namespace android {
37 namespace hardware {
38 namespace sensors {
39 namespace V2_0 {
40 namespace subhal {
41 namespace implementation {
42 
frequency_to_us(unsigned int x)43 static constexpr unsigned int frequency_to_us(unsigned int x) {
44     return (1E6 / x);
45 }
46 
ns_to_frequency(unsigned int x)47 static constexpr unsigned int ns_to_frequency(unsigned int x) {
48     return (1E9 / x);
49 }
50 
51 // SCMI IIO driver gives sensor power in microwatts. Sensor HAL expects
52 // it in mA. Conversion process needs the input voltage for the IMU.
53 // Based on commonly used IMUs, 3.6V is picked as the default.
54 constexpr auto SENSOR_VOLTAGE_DEFAULT = 3.6f;
55 
56 class ISensorsEventCallback {
57   public:
58     virtual ~ISensorsEventCallback() = default;
59     virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0;
60 };
61 
62 // Virtual Base Class for Sensor
63 class SensorBase {
64   public:
65     SensorBase(int32_t sensorHandle, ISensorsEventCallback* callback, SensorType type);
66     virtual ~SensorBase();
67     const SensorInfo& getSensorInfo() const;
68     virtual void batch(int32_t samplingPeriodNs) = 0;
69     virtual void activate(bool enable) = 0;
70     Result flush();
71     void setOperationMode(OperationMode mode);
72     bool supportsDataInjection() const;
73     Result injectEvent(const Event& event);
74 
75   protected:
76     virtual void run() = 0;
77     bool isWakeUpSensor();
78 
79     bool mIsEnabled;
80     int64_t mSamplingPeriodNs;
81     SensorInfo mSensorInfo;
82     std::atomic_bool mStopThread;
83     std::condition_variable mWaitCV;
84     std::mutex mRunMutex;
85     std::thread mRunThread;
86     ISensorsEventCallback* mCallback;
87     OperationMode mMode;
88 };
89 
90 // HWSensorBase represents the actual physical sensor provided as the IIO device
91 class HWSensorBase : public SensorBase {
92   public:
93     HWSensorBase(int32_t sensorHandle, ISensorsEventCallback* callback, SensorType type,
94                  const struct iio_device_data& iio_data);
95     virtual ~HWSensorBase() = 0;
96     void batch(int32_t samplingPeriodNs);
97     void activate(bool enable);
98 
99     struct iio_device_data miio_data;
100 
101   private:
102     ssize_t mscan_size;
103     struct pollfd mpollfd_iio;
104     std::vector<uint8_t> msensor_raw_data;
105 
106     ssize_t calculateScanSize();
107     void run();
108     void processScanData(uint8_t* data, Event* evt);
109 };
110 
111 class Accelerometer : public HWSensorBase {
112   public:
113     Accelerometer(int32_t sensorHandle, ISensorsEventCallback* callback,
114                   const struct iio_device_data& iio_data);
115 };
116 
117 class Gyroscope : public HWSensorBase {
118   public:
119     Gyroscope(int32_t sensorHandle, ISensorsEventCallback* callback,
120               const struct iio_device_data& iio_data);
121 };
122 
123 }  // namespace implementation
124 }  // namespace subhal
125 }  // namespace V2_0
126 }  // namespace sensors
127 }  // namespace hardware
128 }  // namespace android
129 #endif  // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
130