blob: b4d246690d9cb8a498023e8a7133ef7f189353c4 [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#include "HalProxy.h"
18
19#include <android/hardware/sensors/2.0/types.h>
20
Stan Rokita28790672019-08-20 14:32:15 -070021#include <dlfcn.h>
22
23#include <fstream>
24#include <functional>
25
Anthony Stangea689f8a2019-07-30 11:35:48 -040026namespace android {
27namespace hardware {
28namespace sensors {
29namespace V2_0 {
30namespace implementation {
31
Stan Rokita28790672019-08-20 14:32:15 -070032typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*);
33
Anthony Stangea689f8a2019-07-30 11:35:48 -040034// TODO: Use this wake lock name as the prefix to all sensors HAL wake locks acquired.
35// constexpr const char* kWakeLockName = "SensorsHAL_WAKEUP";
36
37// TODO: Use the following class as a starting point for implementing the full HalProxyCallback
38// along with being inspiration for how to implement the ScopedWakelock class.
39/**
40 * Callback class used to provide the HalProxy with the index of which subHal is invoking
41 */
42class SensorsCallbackProxy : public ISensorsCallback {
43 public:
44 SensorsCallbackProxy(wp<HalProxy>& halProxy, int32_t subHalIndex)
45 : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {}
46
47 Return<void> onDynamicSensorsConnected(
48 const hidl_vec<SensorInfo>& dynamicSensorsAdded) override {
49 sp<HalProxy> halProxy(mHalProxy.promote());
50 if (halProxy != nullptr) {
51 return halProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex);
52 }
53 return Return<void>();
54 }
55
56 Return<void> onDynamicSensorsDisconnected(
57 const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
58 sp<HalProxy> halProxy(mHalProxy.promote());
59 if (halProxy != nullptr) {
60 return halProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved,
61 mSubHalIndex);
62 }
63 return Return<void>();
64 }
65
66 private:
67 wp<HalProxy>& mHalProxy;
68 int32_t mSubHalIndex;
69};
70
71HalProxy::HalProxy() {
Stan Rokita7a723542019-08-29 15:47:50 -070072 const char* kMultiHalConfigFile = "/vendor/etc/sensors/hals.conf";
73 initializeSubHalListFromConfigFile(kMultiHalConfigFile);
Stan Rokitadc7a8e72019-08-23 12:35:40 -070074 initializeSensorList();
Anthony Stangea689f8a2019-07-30 11:35:48 -040075}
76
Anthony Stangeaacbf942019-08-30 15:21:34 -040077HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) {
Stan Rokitadc7a8e72019-08-23 12:35:40 -070078 initializeSensorList();
Anthony Stangeaacbf942019-08-30 15:21:34 -040079}
80
Anthony Stangea689f8a2019-07-30 11:35:48 -040081HalProxy::~HalProxy() {
82 // TODO: Join any running threads and clean up FMQs and any other allocated
83 // state.
84}
85
Stan Rokitadc7a8e72019-08-23 12:35:40 -070086Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) {
87 _hidl_cb(mSensorList);
Anthony Stangea689f8a2019-07-30 11:35:48 -040088 return Void();
89}
90
Stan Rokita7a723542019-08-29 15:47:50 -070091Return<Result> HalProxy::setOperationMode(OperationMode mode) {
92 Result result = Result::OK;
93 size_t subHalIndex;
94 for (subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) {
95 ISensorsSubHal* subHal = mSubHalList[subHalIndex];
96 result = subHal->setOperationMode(mode);
97 if (result != Result::OK) {
98 ALOGE("setOperationMode failed for SubHal: %s", subHal->getName().c_str());
99 break;
100 }
101 }
102 if (result != Result::OK) {
103 // Reset the subhal operation modes that have been flipped
104 for (size_t i = 0; i < subHalIndex; i++) {
105 ISensorsSubHal* subHal = mSubHalList[i];
106 subHal->setOperationMode(mCurrentOperationMode);
107 }
108 } else {
109 mCurrentOperationMode = mode;
110 }
111 return result;
Anthony Stangea689f8a2019-07-30 11:35:48 -0400112}
113
Stan Rokita4b4c7b72019-09-10 15:07:59 -0700114Return<Result> HalProxy::activate(int32_t sensorHandle, bool enabled) {
115 return getSubHalForSensorHandle(sensorHandle)
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700116 ->activate(clearSubHalIndex(sensorHandle), enabled);
Anthony Stangea689f8a2019-07-30 11:35:48 -0400117}
118
119Return<Result> HalProxy::initialize(
120 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
121 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
122 const sp<ISensorsCallback>& sensorsCallback) {
123 Result result = Result::OK;
124
125 // TODO: clean up sensor requests, if not already done elsewhere through a death recipient, and
126 // clean up any other resources that exist (FMQs, flags, threads, etc.)
127
128 mDynamicSensorsCallback = sensorsCallback;
129
130 // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
131 mEventQueue =
132 std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */);
133
134 // Create the EventFlag that is used to signal to the framework that sensor events have been
135 // written to the Event FMQ
136 if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
137 result = Result::BAD_VALUE;
138 }
139
140 // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
141 // events have been successfully read and handled by the framework.
142 mWakeLockQueue =
143 std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
144
145 if (!mDynamicSensorsCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
146 result = Result::BAD_VALUE;
147 }
148
149 // TODO: start threads to read wake locks and process events from sub HALs.
150
151 return result;
152}
153
Stan Rokita4b4c7b72019-09-10 15:07:59 -0700154Return<Result> HalProxy::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
155 int64_t maxReportLatencyNs) {
156 return getSubHalForSensorHandle(sensorHandle)
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700157 ->batch(clearSubHalIndex(sensorHandle), samplingPeriodNs, maxReportLatencyNs);
Anthony Stangea689f8a2019-07-30 11:35:48 -0400158}
159
Stan Rokita4b4c7b72019-09-10 15:07:59 -0700160Return<Result> HalProxy::flush(int32_t sensorHandle) {
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700161 return getSubHalForSensorHandle(sensorHandle)->flush(clearSubHalIndex(sensorHandle));
Anthony Stangea689f8a2019-07-30 11:35:48 -0400162}
163
164Return<Result> HalProxy::injectSensorData(const Event& /* event */) {
165 // TODO: Proxy API call to appropriate sub-HAL.
166 return Result::INVALID_OPERATION;
167}
168
169Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& /* mem */,
170 registerDirectChannel_cb _hidl_cb) {
171 // TODO: During init, discover the first sub-HAL in the config that has sensors with direct
172 // channel support, if any, and proxy the API call there.
173 _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */);
174 return Return<void>();
175}
176
177Return<Result> HalProxy::unregisterDirectChannel(int32_t /* channelHandle */) {
178 // TODO: During init, discover the first sub-HAL in the config that has sensors with direct
179 // channel support, if any, and proxy the API call there.
180 return Result::INVALID_OPERATION;
181}
182
183Return<void> HalProxy::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */,
184 RateLevel /* rate */, configDirectReport_cb _hidl_cb) {
185 // TODO: During init, discover the first sub-HAL in the config that has sensors with direct
186 // channel support, if any, and proxy the API call there.
187 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
188 return Return<void>();
189}
190
191Return<void> HalProxy::debug(const hidl_handle& /* fd */, const hidl_vec<hidl_string>& /* args */) {
192 // TODO: output debug information
193 return Return<void>();
194}
195
196Return<void> HalProxy::onDynamicSensorsConnected(
197 const hidl_vec<SensorInfo>& /* dynamicSensorsAdded */, int32_t /* subHalIndex */) {
198 // TODO: Map the SensorInfo to the global list and then invoke the framework's callback.
199 return Return<void>();
200}
201
202Return<void> HalProxy::onDynamicSensorsDisconnected(
203 const hidl_vec<int32_t>& /* dynamicSensorHandlesRemoved */, int32_t /* subHalIndex */) {
204 // TODO: Unmap the SensorInfo from the global list and then invoke the framework's callback.
205 return Return<void>();
206}
207
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700208void HalProxy::initializeSubHalListFromConfigFile(const char* configFileName) {
209 std::ifstream subHalConfigStream(configFileName);
210 if (!subHalConfigStream) {
Stan Rokita7a723542019-08-29 15:47:50 -0700211 ALOGE("Failed to load subHal config file: %s", configFileName);
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700212 } else {
213 std::string subHalLibraryFile;
214 while (subHalConfigStream >> subHalLibraryFile) {
215 void* handle = dlopen(subHalLibraryFile.c_str(), RTLD_NOW);
216 if (handle == nullptr) {
Stan Rokita7a723542019-08-29 15:47:50 -0700217 ALOGE("dlopen failed for library: %s", subHalLibraryFile.c_str());
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700218 } else {
219 SensorsHalGetSubHalFunc* sensorsHalGetSubHalPtr =
220 (SensorsHalGetSubHalFunc*)dlsym(handle, "sensorsHalGetSubHal");
221 if (sensorsHalGetSubHalPtr == nullptr) {
Stan Rokita7a723542019-08-29 15:47:50 -0700222 ALOGE("Failed to locate sensorsHalGetSubHal function for library: %s",
223 subHalLibraryFile.c_str());
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700224 } else {
225 std::function<SensorsHalGetSubHalFunc> sensorsHalGetSubHal =
226 *sensorsHalGetSubHalPtr;
227 uint32_t version;
228 ISensorsSubHal* subHal = sensorsHalGetSubHal(&version);
229 if (version != SUB_HAL_2_0_VERSION) {
Stan Rokita7a723542019-08-29 15:47:50 -0700230 ALOGE("SubHal version was not 2.0 for library: %s",
231 subHalLibraryFile.c_str());
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700232 } else {
233 ALOGV("Loaded SubHal from library: %s", subHalLibraryFile.c_str());
234 mSubHalList.push_back(subHal);
235 }
236 }
237 }
238 }
239 }
240}
241
242void HalProxy::initializeSensorList() {
243 for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) {
244 ISensorsSubHal* subHal = mSubHalList[subHalIndex];
245 auto result = subHal->getSensorsList([&](const auto& list) {
246 for (SensorInfo sensor : list) {
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700247 if ((sensor.sensorHandle & kSensorHandleSubHalIndexMask) != 0) {
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700248 ALOGE("SubHal sensorHandle's first byte was not 0");
249 } else {
250 ALOGV("Loaded sensor: %s", sensor.name.c_str());
251 sensor.sensorHandle |= (subHalIndex << 24);
Stan Rokita7a723542019-08-29 15:47:50 -0700252 setDirectChannelFlags(&sensor, subHal);
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700253 mSensorList.push_back(sensor);
254 }
255 }
256 });
257 if (!result.isOk()) {
258 ALOGE("getSensorsList call failed for SubHal: %s", subHal->getName().c_str());
259 }
260 }
261}
262
Stan Rokita7a723542019-08-29 15:47:50 -0700263void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal) {
264 bool sensorSupportsDirectChannel =
265 (sensorInfo->flags & (V1_0::SensorFlagBits::MASK_DIRECT_REPORT |
266 V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL)) != 0;
267 if (mDirectChannelSubHal == nullptr && sensorSupportsDirectChannel) {
268 mDirectChannelSubHal = subHal;
269 } else if (mDirectChannelSubHal != nullptr && subHal != mDirectChannelSubHal) {
270 // disable direct channel capability for sensors in subHals that are not
271 // the only one we will enable
272 sensorInfo->flags &= ~(V1_0::SensorFlagBits::MASK_DIRECT_REPORT |
273 V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL);
274 }
275}
276
Stan Rokita16385312019-09-10 14:54:36 -0700277ISensorsSubHal* HalProxy::getSubHalForSensorHandle(uint32_t sensorHandle) {
278 return mSubHalList[static_cast<size_t>(sensorHandle >> 24)];
279}
280
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700281uint32_t HalProxy::clearSubHalIndex(uint32_t sensorHandle) {
282 return sensorHandle & (~kSensorHandleSubHalIndexMask);
Stan Rokita16385312019-09-10 14:54:36 -0700283}
284
Anthony Stangea689f8a2019-07-30 11:35:48 -0400285} // namespace implementation
286} // namespace V2_0
287} // namespace sensors
288} // namespace hardware
289} // namespace android