blob: 41c35485b69d42c503fb6ef36cb7ccb20b1c533c [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 Rokita28790672019-08-20 14:32:15 -070072 const char* kMultiHalConfigFilePath = "/vendor/etc/sensors/hals.conf";
73 std::ifstream subHalConfigStream(kMultiHalConfigFilePath);
74 if (!subHalConfigStream) {
75 LOG_FATAL("Failed to load subHal config file: %s", kMultiHalConfigFilePath);
76 } else {
77 std::string subHalLibraryFile;
78 while (subHalConfigStream >> subHalLibraryFile) {
79 void* handle = dlopen(subHalLibraryFile.c_str(), RTLD_NOW);
80 if (handle == nullptr) {
81 LOG_FATAL("dlopen failed for library: %s", subHalLibraryFile.c_str());
82 } else {
83 SensorsHalGetSubHalFunc* sensorsHalGetSubHalPtr =
84 (SensorsHalGetSubHalFunc*)dlsym(handle, "sensorsHalGetSubHal");
85 if (sensorsHalGetSubHalPtr == nullptr) {
86 LOG_FATAL("Failed to locate sensorsHalGetSubHal function for library: %s",
87 subHalLibraryFile.c_str());
88 } else {
89 std::function<SensorsHalGetSubHalFunc> sensorsHalGetSubHal =
90 *sensorsHalGetSubHalPtr;
91 uint32_t version;
92 ISensorsSubHal* subHal = sensorsHalGetSubHal(&version);
93 if (version != SUB_HAL_2_0_VERSION) {
94 LOG_FATAL("SubHal version was not 2.0 for library: %s",
95 subHalLibraryFile.c_str());
96 } else {
97 ALOGI("Loaded SubHal from library: %s", subHalLibraryFile.c_str());
98 mSubHalList.push_back(subHal);
99 }
100 }
101 }
102 }
103 }
104 // TODO: Discover sensors
Anthony Stangea689f8a2019-07-30 11:35:48 -0400105}
106
107HalProxy::~HalProxy() {
108 // TODO: Join any running threads and clean up FMQs and any other allocated
109 // state.
110}
111
112Return<void> HalProxy::getSensorsList(getSensorsList_cb /* _hidl_cb */) {
113 // TODO: Output sensors list created as part of HalProxy().
114 return Void();
115}
116
117Return<Result> HalProxy::setOperationMode(OperationMode /* mode */) {
118 // TODO: Proxy API call to all sub-HALs and return appropriate result.
119 return Result::INVALID_OPERATION;
120}
121
122Return<Result> HalProxy::activate(int32_t /* sensorHandle */, bool /* enabled */) {
123 // TODO: Proxy API call to appropriate sub-HAL.
124 return Result::INVALID_OPERATION;
125}
126
127Return<Result> HalProxy::initialize(
128 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
129 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
130 const sp<ISensorsCallback>& sensorsCallback) {
131 Result result = Result::OK;
132
133 // TODO: clean up sensor requests, if not already done elsewhere through a death recipient, and
134 // clean up any other resources that exist (FMQs, flags, threads, etc.)
135
136 mDynamicSensorsCallback = sensorsCallback;
137
138 // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
139 mEventQueue =
140 std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */);
141
142 // Create the EventFlag that is used to signal to the framework that sensor events have been
143 // written to the Event FMQ
144 if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
145 result = Result::BAD_VALUE;
146 }
147
148 // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
149 // events have been successfully read and handled by the framework.
150 mWakeLockQueue =
151 std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
152
153 if (!mDynamicSensorsCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
154 result = Result::BAD_VALUE;
155 }
156
157 // TODO: start threads to read wake locks and process events from sub HALs.
158
159 return result;
160}
161
162Return<Result> HalProxy::batch(int32_t /* sensorHandle */, int64_t /* samplingPeriodNs */,
163 int64_t /* maxReportLatencyNs */) {
164 // TODO: Proxy API call to appropriate sub-HAL.
165 return Result::INVALID_OPERATION;
166}
167
168Return<Result> HalProxy::flush(int32_t /* sensorHandle */) {
169 // TODO: Proxy API call to appropriate sub-HAL.
170 return Result::INVALID_OPERATION;
171}
172
173Return<Result> HalProxy::injectSensorData(const Event& /* event */) {
174 // TODO: Proxy API call to appropriate sub-HAL.
175 return Result::INVALID_OPERATION;
176}
177
178Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& /* mem */,
179 registerDirectChannel_cb _hidl_cb) {
180 // TODO: During init, discover the first sub-HAL in the config that has sensors with direct
181 // channel support, if any, and proxy the API call there.
182 _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */);
183 return Return<void>();
184}
185
186Return<Result> HalProxy::unregisterDirectChannel(int32_t /* channelHandle */) {
187 // TODO: During init, discover the first sub-HAL in the config that has sensors with direct
188 // channel support, if any, and proxy the API call there.
189 return Result::INVALID_OPERATION;
190}
191
192Return<void> HalProxy::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */,
193 RateLevel /* rate */, configDirectReport_cb _hidl_cb) {
194 // TODO: During init, discover the first sub-HAL in the config that has sensors with direct
195 // channel support, if any, and proxy the API call there.
196 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
197 return Return<void>();
198}
199
200Return<void> HalProxy::debug(const hidl_handle& /* fd */, const hidl_vec<hidl_string>& /* args */) {
201 // TODO: output debug information
202 return Return<void>();
203}
204
205Return<void> HalProxy::onDynamicSensorsConnected(
206 const hidl_vec<SensorInfo>& /* dynamicSensorsAdded */, int32_t /* subHalIndex */) {
207 // TODO: Map the SensorInfo to the global list and then invoke the framework's callback.
208 return Return<void>();
209}
210
211Return<void> HalProxy::onDynamicSensorsDisconnected(
212 const hidl_vec<int32_t>& /* dynamicSensorHandlesRemoved */, int32_t /* subHalIndex */) {
213 // TODO: Unmap the SensorInfo from the global list and then invoke the framework's callback.
214 return Return<void>();
215}
216
217} // namespace implementation
218} // namespace V2_0
219} // namespace sensors
220} // namespace hardware
221} // namespace android