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