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
27 #include "SensorThread.h"
28 #include "iio_utils.h"
29 #include "sensor_hal_configuration_V1_0.h"
30
31 #define NUM_OF_CHANNEL_SUPPORTED 4
32 // Subtract the timestamp channel to get the number of data channels
33 #define NUM_OF_DATA_CHANNELS NUM_OF_CHANNEL_SUPPORTED - 1
34
35 using ::android::hardware::sensors::V1_0::AdditionalInfo;
36 using ::android::hardware::sensors::V1_0::Event;
37 using ::android::hardware::sensors::V1_0::OperationMode;
38 using ::android::hardware::sensors::V1_0::Result;
39 using ::android::hardware::sensors::V1_0::SensorInfo;
40 using ::android::hardware::sensors::V1_0::SensorType;
41 using ::sensor::hal::configuration::V1_0::Configuration;
42
43 namespace android {
44 namespace hardware {
45 namespace sensors {
46 namespace V2_0 {
47 namespace subhal {
48 namespace implementation {
49
frequency_to_us(unsigned int x)50 static constexpr unsigned int frequency_to_us(unsigned int x) {
51 return (1E6 / x);
52 }
53
ns_to_frequency(unsigned int x)54 static constexpr unsigned int ns_to_frequency(unsigned int x) {
55 return (1E9 / x);
56 }
57
58 // SCMI IIO driver gives sensor power in microwatts. Sensor HAL expects
59 // it in mA. Conversion process needs the input voltage for the IMU.
60 // Based on commonly used IMUs, 3.6V is picked as the default.
61 constexpr auto SENSOR_VOLTAGE_DEFAULT = 3.6f;
62
63 class ISensorsEventCallback {
64 public:
65 virtual ~ISensorsEventCallback() = default;
66 virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0;
67 };
68
69 // Virtual Base Class for Sensor
70 class SensorBase {
71 public:
72 SensorBase(int32_t sensorHandle, ISensorsEventCallback* callback, SensorType type);
73 virtual ~SensorBase();
74 const SensorInfo& getSensorInfo() const;
75 virtual void batch(int32_t samplingPeriodNs) = 0;
76 virtual void activate(bool enable) = 0;
77 virtual Result flush();
78 void setOperationMode(OperationMode mode);
79 bool supportsDataInjection() const;
80 Result injectEvent(const Event& event);
81
82 bool isEnabled() const;
83 OperationMode getOperationMode() const;
84
85 protected:
86 friend class SensorThread;
87
88 virtual void pollSensor() = 0;
89 bool isWakeUpSensor();
90
91 bool mIsEnabled;
92 int64_t mSamplingPeriodNs;
93 SensorInfo mSensorInfo;
94 ISensorsEventCallback* mCallback;
95 OperationMode mMode;
96 SensorThread mSensorThread;
97 };
98
99 class SensorThread;
100
101 // HWSensorBase represents the actual physical sensor provided as the IIO device
102 class HWSensorBase : public SensorBase {
103 public:
104 static HWSensorBase* buildSensor(int32_t sensorHandle, ISensorsEventCallback* callback,
105 const struct iio_device_data& iio_data,
106 const std::optional<std::vector<Configuration>>& config);
107 ~HWSensorBase();
108 void batch(int32_t samplingPeriodNs);
109 void activate(bool enable);
110 Result flush();
111 struct iio_device_data mIioData;
112
113 protected:
114 void pollSensor() override;
115
116 private:
117 void idleLoop();
118 void pollForEvents();
119
120 static constexpr uint8_t LOCATION_X_IDX = 3;
121 static constexpr uint8_t LOCATION_Y_IDX = 7;
122 static constexpr uint8_t LOCATION_Z_IDX = 11;
123 static constexpr uint8_t ROTATION_X_IDX = 0;
124 static constexpr uint8_t ROTATION_Y_IDX = 1;
125 static constexpr uint8_t ROTATION_Z_IDX = 2;
126
127 ssize_t mScanSize;
128 struct pollfd mPollFdIio;
129 std::vector<uint8_t> mSensorRawData;
130 int64_t mXMap, mYMap, mZMap;
131 bool mXNegate, mYNegate, mZNegate;
132 std::vector<AdditionalInfo> mAdditionalInfoFrames;
133
134 HWSensorBase(int32_t sensorHandle, ISensorsEventCallback* callback,
135 const struct iio_device_data& iio_data,
136 const std::optional<std::vector<Configuration>>& config);
137
138 ssize_t calculateScanSize();
139 void run();
140 void setOrientation(std::optional<std::vector<Configuration>> config);
141 void processScanData(uint8_t* data, Event* evt);
142 void setAxisDefaultValues();
143 status_t setAdditionalInfoFrames(const std::optional<std::vector<Configuration>>& config);
144 void sendAdditionalInfoReport();
145 status_t getSensorPlacement(AdditionalInfo* sensorPlacement,
146 const std::optional<std::vector<Configuration>>& config);
147 };
148 } // namespace implementation
149 } // namespace subhal
150 } // namespace V2_0
151 } // namespace sensors
152 } // namespace hardware
153 } // namespace android
154 #endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
155