blob: 4ecb58b5a57d336a8697cc785077d629530e7a61 [file] [log] [blame]
Anthony Stangea689f8a2019-07-30 11:35:48 -04001/*
2 * Copyright (C) 2019 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
17#pragma once
18
19#include "SubHal.h"
20
21#include <android/hardware/sensors/2.0/ISensors.h>
22#include <fmq/MessageQueue.h>
23#include <hardware_legacy/power.h>
24#include <hidl/MQDescriptor.h>
25#include <hidl/Status.h>
26
27namespace android {
28namespace hardware {
29namespace sensors {
30namespace V2_0 {
31namespace implementation {
32
33using ::android::sp;
34using ::android::hardware::EventFlag;
35using ::android::hardware::hidl_string;
36using ::android::hardware::hidl_vec;
37using ::android::hardware::MessageQueue;
38using ::android::hardware::MQDescriptor;
39using ::android::hardware::Return;
40using ::android::hardware::Void;
41
Stan Rokitadc7a8e72019-08-23 12:35:40 -070042class HalProxy : public ISensors {
43 public:
Anthony Stangea689f8a2019-07-30 11:35:48 -040044 using Event = ::android::hardware::sensors::V1_0::Event;
45 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
46 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
47 using Result = ::android::hardware::sensors::V1_0::Result;
48 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
49
Anthony Stangeaacbf942019-08-30 15:21:34 -040050 explicit HalProxy();
51 // Test only constructor.
52 explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList);
Anthony Stangea689f8a2019-07-30 11:35:48 -040053 ~HalProxy();
54
55 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
56 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
57
58 Return<Result> setOperationMode(OperationMode mode) override;
59
60 Return<Result> activate(int32_t sensorHandle, bool enabled) override;
61
62 Return<Result> initialize(
63 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
64 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
65 const sp<ISensorsCallback>& sensorsCallback) override;
66
67 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
68 int64_t maxReportLatencyNs) override;
69
70 Return<Result> flush(int32_t sensorHandle) override;
71
72 Return<Result> injectSensorData(const Event& event) override;
73
74 Return<void> registerDirectChannel(const SharedMemInfo& mem,
75 registerDirectChannel_cb _hidl_cb) override;
76
77 Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
78
79 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
80 configDirectReport_cb _hidl_cb) override;
81
82 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
83
Anthony Stangeaacbf942019-08-30 15:21:34 -040084 // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change
Anthony Stangea689f8a2019-07-30 11:35:48 -040085 // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
86 // via the binder, these methods are invoked from a callback provided to sub-HALs inside the
87 // same process as the HalProxy, but potentially running on different threads.
88 Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded,
89 int32_t subHalIndex);
90
91 Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
92 int32_t subHalIndex);
93
94 private:
95 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
96 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
97
98 /**
99 * The Event FMQ where sensor events are written
100 */
101 std::unique_ptr<EventMessageQueue> mEventQueue;
102
103 /**
104 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
105 */
106 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
107
108 /**
109 * Event Flag to signal to the framework when sensor events are available to be read
110 */
111 EventFlag* mEventQueueFlag;
112
113 /**
114 * Callback to the sensors framework to inform it that new sensors have been added or removed.
115 */
116 sp<ISensorsCallback> mDynamicSensorsCallback;
Stan Rokita28790672019-08-20 14:32:15 -0700117
118 /**
119 * SubHal object pointers that have been saved from vendor dynamic libraries.
120 */
121 std::vector<ISensorsSubHal*> mSubHalList;
Stan Rokita16385312019-09-10 14:54:36 -0700122
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700123 /**
124 * List of SensorInfo objects that contains the sensor info from subhals as
125 * well as the modified sensor handle for the framework.
126 *
127 * The subhal index is encoded in the first byte of the sensor handle and
Stan Rokita7a723542019-08-29 15:47:50 -0700128 * the remaining bytes are generated by the subhal to identify the sensor.
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700129 */
130 std::vector<SensorInfo> mSensorList;
131
Stan Rokita7a723542019-08-29 15:47:50 -0700132 //! The current operation mode for all subhals.
133 OperationMode mCurrentOperationMode = OperationMode::NORMAL;
134
135 //! The single subHal that supports directChannel reporting.
136 ISensorsSubHal* mDirectChannelSubHal = nullptr;
137
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700138 //! The bit mask used to get the subhal index from a sensor handle.
139 static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000;
140
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700141 /**
142 * Initialize the list of SubHal objects in mSubHalList by reading from dynamic libraries
143 * listed in a config file.
144 */
145 void initializeSubHalListFromConfigFile(const char* configFileName);
146
147 /**
148 * Initialize the list of SensorInfo objects in mSensorList by getting sensors from each
149 * subhal.
150 */
151 void initializeSensorList();
152
Stan Rokita7a723542019-08-29 15:47:50 -0700153 /**
154 * Clear direct channel flags if the HalProxy has already chosen a subhal as its direct channel
155 * subhal. Set the directChannelSubHal pointer to the subHal passed in if this is the first
156 * direct channel enabled sensor seen.
157 *
158 * @param sensorInfo The SensorInfo object that may be altered to have direct channel support
159 * disabled.
160 * @param subHal The subhal pointer that the current sensorInfo object came from.
161 */
162 void setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal);
163
Stan Rokita16385312019-09-10 14:54:36 -0700164 /*
165 * Get the subhal pointer which can be found by indexing into the mSubHalList vector
166 * using the index from the first byte of sensorHandle.
167 *
168 * @param sensorHandle The handle used to identify a sensor in one of the subhals.
169 */
170 ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle);
171
172 /*
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700173 * Clear out the subhal index bytes from a sensorHandle.
Stan Rokita16385312019-09-10 14:54:36 -0700174 *
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700175 * @param sensorHandle The sensor handle to modify.
Stan Rokita16385312019-09-10 14:54:36 -0700176 *
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700177 * @return The modified version of the sensor handle.
Stan Rokita16385312019-09-10 14:54:36 -0700178 */
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700179 static uint32_t clearSubHalIndex(uint32_t sensorHandle);
Anthony Stangea689f8a2019-07-30 11:35:48 -0400180};
181
182} // namespace implementation
183} // namespace V2_0
184} // namespace sensors
185} // namespace hardware
186} // namespace android