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_1_SENSORS_SUBHAL_H
17 #define ANDROID_HARDWARE_SENSORS_V2_1_SENSORS_SUBHAL_H
18 
19 #include <vector>
20 #include "Sensor.h"
21 #include "SubHal.h"
22 
23 using ::android::hardware::sensors::V2_1::SensorType;
24 
25 namespace android {
26 namespace hardware {
27 namespace sensors {
28 namespace V2_1 {
29 namespace subhal {
30 namespace implementation {
31 
32 using ::android::hardware::sensors::V1_0::OperationMode;
33 using ::android::hardware::sensors::V1_0::Result;
34 using ::android::hardware::sensors::V2_1::implementation::IHalProxyCallback;
35 using ::android::hardware::sensors::V2_1::implementation::ISensorsSubHal;
36 using ::android::hardware::sensors::V2_1::subhal::implementation::ISensorsEventCallback;
37 using ::sensor::hal::configuration::V1_0::Configuration;
38 
39 /**
40  * Implementation of a ISensorsSubHal that can be used as a reference HAL implementation of sensors
41  * multihal 2.0. See the README file for more details.
42  */
43 class SensorsSubHal : public ISensorsSubHal, public ISensorsEventCallback {
44     using Event = ::android::hardware::sensors::V2_1::Event;
45     using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
46     using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
47     using ScopedWakelock = ::android::hardware::sensors::V2_0::implementation::ScopedWakelock;
48 
49   public:
50     SensorsSubHal();
51 
52     // Methods from ::android::hardware::sensors::V2_1::ISensors follow.
53     Return<void> getSensorsList_2_1(getSensorsList_2_1_cb _hidl_cb) override;
54 
55     Return<Result> setOperationMode(OperationMode mode) override;
56 
getOperationMode()57     OperationMode getOperationMode() const { return mCurrentOperationMode; }
58 
59     Return<Result> activate(int32_t sensorHandle, bool enabled) override;
60 
61     Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
62                          int64_t maxReportLatencyNs) override;
63 
64     Return<Result> flush(int32_t sensorHandle) override;
65 
66     Return<Result> injectSensorData_2_1(const Event& event) override;
67 
68     Return<void> registerDirectChannel(const SharedMemInfo& mem,
69                                        registerDirectChannel_cb _hidl_cb) override;
70 
71     Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
72 
73     Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
74                                     configDirectReport_cb _hidl_cb) override;
75 
76     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
77 
78     // Methods from ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal follow.
getName()79     const std::string getName() override { return "Google-IIO-SensorsSubhal"; }
80 
81     Return<Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) override;
82 
83     // Method from ISensorsEventCallback.
84     void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) override;
85 
86     ScopedWakelock createScopedWakelock(bool lock) override;
87 
88   protected:
89     void AddSensor(const struct iio_device_data& iio_data,
90                    const std::optional<std::vector<Configuration>>& config);
91 
92     /**
93      * A map of the available sensors
94      */
95     std::map<int32_t, std::unique_ptr<SensorBase>> mSensors;
96 
97     /**
98      * Callback used to communicate to the HalProxy when dynamic sensors are connected /
99      * disconnected, sensor events need to be sent to the framework, and when a wakelock should be
100      * acquired.
101      */
102     sp<IHalProxyCallback> mCallback;
103 
104   private:
105     /**
106      * The current operation mode of the multihal framework. Ensures that all subhals are set to
107      * the same operation mode.
108      */
109     OperationMode mCurrentOperationMode = OperationMode::NORMAL;
110 
111     /**
112      * The next available sensor handle
113      */
114     int32_t mNextHandle;
115 };
116 
117 }  // namespace implementation
118 }  // namespace subhal
119 }  // namespace V2_1
120 }  // namespace sensors
121 }  // namespace hardware
122 }  // namespace android
123 #endif
124