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