1 /*
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 "SensorsSubHal.h"
18
19 #include <android/hardware/sensors/2.1/types.h>
20 #include <log/log.h>
21
22 #ifdef SUB_HAL_VERSION_2_0
sensorsHalGetSubHal(uint32_t * version)23 ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal* sensorsHalGetSubHal(
24 uint32_t* version) {
25 #if defined SUPPORT_CONTINUOUS_SENSORS && defined SUPPORT_ON_CHANGE_SENSORS
26 static ::android::hardware::sensors::V2_1::subhal::implementation::AllSensorsSubHal<
27 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_0>
28 subHal;
29 #elif defined SUPPORT_CONTINUOUS_SENSORS
30 static ::android::hardware::sensors::V2_1::subhal::implementation::ContinuousSensorsSubHal<
31 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_0>
32 subHal;
33 #elif defined SUPPORT_ON_CHANGE_SENSORS
34 static ::android::hardware::sensors::V2_1::subhal::implementation::OnChangeSensorsSubHal<
35 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_0>
36 subHal;
37 #else
38 static ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHal<
39 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_0>
40 subHal;
41 #endif // defined SUPPORT_CONTINUOUS_SENSORS && defined SUPPORT_ON_CHANGE_SENSORS
42 *version = SUB_HAL_2_0_VERSION;
43 return &subHal;
44 }
45
46 #else // SUB_HAL_VERSION_2_0
47
sensorsHalGetSubHal_2_1(uint32_t * version)48 ::android::hardware::sensors::V2_1::implementation::ISensorsSubHal* sensorsHalGetSubHal_2_1(
49 uint32_t* version) {
50 #if defined SUPPORT_CONTINUOUS_SENSORS && defined SUPPORT_ON_CHANGE_SENSORS
51 static ::android::hardware::sensors::V2_1::subhal::implementation::AllSensorsSubHal<
52 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_1>
53 subHal;
54 #elif defined SUPPORT_CONTINUOUS_SENSORS
55 static ::android::hardware::sensors::V2_1::subhal::implementation::ContinuousSensorsSubHal<
56 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_1>
57 subHal;
58 #elif defined SUPPORT_ON_CHANGE_SENSORS
59 static ::android::hardware::sensors::V2_1::subhal::implementation::OnChangeSensorsSubHal<
60 ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_1>
61 subHal;
62 #else
63 static ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_1 subHal;
64 #endif // defined SUPPORT_CONTINUOUS_SENSORS && defined SUPPORT_ON_CHANGE_SENSORS
65 *version = SUB_HAL_2_1_VERSION;
66 return &subHal;
67 }
68
69 #endif // SUB_HAL_VERSION_2_0
70
71 namespace android {
72 namespace hardware {
73 namespace sensors {
74 namespace V2_1 {
75 namespace subhal {
76 namespace implementation {
77
78 using ::android::hardware::Void;
79 using ::android::hardware::sensors::V1_0::OperationMode;
80 using ::android::hardware::sensors::V1_0::RateLevel;
81 using ::android::hardware::sensors::V1_0::Result;
82 using ::android::hardware::sensors::V1_0::SharedMemInfo;
83 using ::android::hardware::sensors::V2_0::SensorTimeout;
84 using ::android::hardware::sensors::V2_0::WakeLockQueueFlagBits;
85 using ::android::hardware::sensors::V2_0::implementation::ScopedWakelock;
86 using ::android::hardware::sensors::V2_1::Event;
87
ISensorsSubHalBase()88 ISensorsSubHalBase::ISensorsSubHalBase() : mCallback(nullptr), mNextHandle(1) {}
89
90 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
getSensorsList(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb)91 Return<void> ISensorsSubHalBase::getSensorsList(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) {
92 std::vector<SensorInfo> sensors;
93 for (const auto& sensor : mSensors) {
94 sensors.push_back(sensor.second->getSensorInfo());
95 }
96
97 _hidl_cb(sensors);
98 return Void();
99 }
100
setOperationMode(OperationMode mode)101 Return<Result> ISensorsSubHalBase::setOperationMode(OperationMode mode) {
102 for (auto sensor : mSensors) {
103 sensor.second->setOperationMode(mode);
104 }
105 mCurrentOperationMode = mode;
106 return Result::OK;
107 }
108
activate(int32_t sensorHandle,bool enabled)109 Return<Result> ISensorsSubHalBase::activate(int32_t sensorHandle, bool enabled) {
110 auto sensor = mSensors.find(sensorHandle);
111 if (sensor != mSensors.end()) {
112 sensor->second->activate(enabled);
113 return Result::OK;
114 }
115 return Result::BAD_VALUE;
116 }
117
batch(int32_t sensorHandle,int64_t samplingPeriodNs,int64_t)118 Return<Result> ISensorsSubHalBase::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
119 int64_t /* maxReportLatencyNs */) {
120 auto sensor = mSensors.find(sensorHandle);
121 if (sensor != mSensors.end()) {
122 sensor->second->batch(samplingPeriodNs);
123 return Result::OK;
124 }
125 return Result::BAD_VALUE;
126 }
127
flush(int32_t sensorHandle)128 Return<Result> ISensorsSubHalBase::flush(int32_t sensorHandle) {
129 auto sensor = mSensors.find(sensorHandle);
130 if (sensor != mSensors.end()) {
131 return sensor->second->flush();
132 }
133 return Result::BAD_VALUE;
134 }
135
injectSensorData(const Event & event)136 Return<Result> ISensorsSubHalBase::injectSensorData(const Event& event) {
137 auto sensor = mSensors.find(event.sensorHandle);
138 if (sensor != mSensors.end()) {
139 return sensor->second->injectEvent(event);
140 }
141
142 return Result::BAD_VALUE;
143 }
144
registerDirectChannel(const SharedMemInfo &,V2_0::ISensors::registerDirectChannel_cb _hidl_cb)145 Return<void> ISensorsSubHalBase::registerDirectChannel(
146 const SharedMemInfo& /* mem */, V2_0::ISensors::registerDirectChannel_cb _hidl_cb) {
147 _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */);
148 return Return<void>();
149 }
150
unregisterDirectChannel(int32_t)151 Return<Result> ISensorsSubHalBase::unregisterDirectChannel(int32_t /* channelHandle */) {
152 return Result::INVALID_OPERATION;
153 }
154
configDirectReport(int32_t,int32_t,RateLevel,V2_0::ISensors::configDirectReport_cb _hidl_cb)155 Return<void> ISensorsSubHalBase::configDirectReport(
156 int32_t /* sensorHandle */, int32_t /* channelHandle */, RateLevel /* rate */,
157 V2_0::ISensors::configDirectReport_cb _hidl_cb) {
158 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
159 return Return<void>();
160 }
161
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & args)162 Return<void> ISensorsSubHalBase::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) {
163 if (fd.getNativeHandle() == nullptr || fd->numFds < 1) {
164 ALOGE("%s: missing fd for writing", __FUNCTION__);
165 return Void();
166 }
167
168 FILE* out = fdopen(dup(fd->data[0]), "w");
169
170 if (args.size() != 0) {
171 fprintf(out,
172 "Note: sub-HAL %s currently does not support args. Input arguments are "
173 "ignored.\n",
174 getName().c_str());
175 }
176
177 std::ostringstream stream;
178 stream << "Available sensors:" << std::endl;
179 for (auto sensor : mSensors) {
180 SensorInfo info = sensor.second->getSensorInfo();
181 stream << "Name: " << info.name << std::endl;
182 stream << "Min delay: " << info.minDelay << std::endl;
183 stream << "Flags: " << info.flags << std::endl;
184 }
185 stream << std::endl;
186
187 fprintf(out, "%s", stream.str().c_str());
188
189 fclose(out);
190 return Return<void>();
191 }
192
initialize(std::unique_ptr<IHalProxyCallbackWrapperBase> & halProxyCallback)193 Return<Result> ISensorsSubHalBase::initialize(
194 std::unique_ptr<IHalProxyCallbackWrapperBase>& halProxyCallback) {
195 mCallback = std::move(halProxyCallback);
196 setOperationMode(OperationMode::NORMAL);
197 return Result::OK;
198 }
199
postEvents(const std::vector<Event> & events,bool wakeup)200 void ISensorsSubHalBase::postEvents(const std::vector<Event>& events, bool wakeup) {
201 ScopedWakelock wakelock = mCallback->createScopedWakelock(wakeup);
202 mCallback->postEvents(events, std::move(wakelock));
203 }
204
setOperationMode(OperationMode)205 Return<Result> SetOperationModeFailingSensorsSubHal::setOperationMode(OperationMode /*mode*/) {
206 return Result::BAD_VALUE;
207 }
208
getSensorsList(getSensorsList_cb _hidl_cb)209 Return<void> AllSupportDirectChannelSensorsSubHal::getSensorsList(getSensorsList_cb _hidl_cb) {
210 std::vector<SensorInfo> sensors;
211 for (const auto& sensor : mSensors) {
212 SensorInfo sensorInfo = sensor.second->getSensorInfo();
213 sensorInfo.flags |= V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL;
214 sensorInfo.flags |= V1_0::SensorFlagBits::MASK_DIRECT_REPORT;
215 sensors.push_back(sensorInfo);
216 }
217 _hidl_cb(V2_1::implementation::convertToOldSensorInfos(sensors));
218 return Void();
219 }
220
getSensorsList(getSensorsList_cb _hidl_cb)221 Return<void> DoesNotSupportDirectChannelSensorsSubHal::getSensorsList(getSensorsList_cb _hidl_cb) {
222 std::vector<SensorInfo> sensors;
223 for (const auto& sensor : mSensors) {
224 SensorInfo sensorInfo = sensor.second->getSensorInfo();
225 sensorInfo.flags &= ~static_cast<uint32_t>(V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL);
226 sensorInfo.flags &= ~static_cast<uint32_t>(V1_0::SensorFlagBits::MASK_DIRECT_REPORT);
227 sensors.push_back(sensorInfo);
228 }
229 _hidl_cb(V2_1::implementation::convertToOldSensorInfos(sensors));
230 return Void();
231 }
232
addDynamicSensors(const std::vector<SensorInfo> & sensorsAdded)233 void AddAndRemoveDynamicSensorsSubHal::addDynamicSensors(
234 const std::vector<SensorInfo>& sensorsAdded) {
235 mCallback->onDynamicSensorsConnected(sensorsAdded);
236 }
237
removeDynamicSensors(const std::vector<int32_t> & sensorHandlesRemoved)238 void AddAndRemoveDynamicSensorsSubHal::removeDynamicSensors(
239 const std::vector<int32_t>& sensorHandlesRemoved) {
240 mCallback->onDynamicSensorsDisconnected(sensorHandlesRemoved);
241 }
242
243 } // namespace implementation
244 } // namespace subhal
245 } // namespace V2_1
246 } // namespace sensors
247 } // namespace hardware
248 } // namespace android
249