blob: da3b2758d12669681c4513008975fe1fa40a27b9 [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 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 */
Peng Xu3889e6e2017-03-02 19:10:38 -080016#include "SensorDevice.h"
17#include "SensorService.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080018
Steven Morelandd15c0302016-12-20 11:14:50 -080019#include <android-base/logging.h>
Peng Xu3889e6e2017-03-02 19:10:38 -080020#include <sensors/convert.h>
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000021#include <utils/Atomic.h>
22#include <utils/Errors.h>
23#include <utils/Singleton.h>
Steven Morelandd3335112016-12-20 11:14:50 -080024
Peng Xu3889e6e2017-03-02 19:10:38 -080025#include <chrono>
26#include <cinttypes>
27#include <thread>
Steven Morelandd15c0302016-12-20 11:14:50 -080028
29using android::hardware::hidl_vec;
30
31using namespace android::hardware::sensors::V1_0;
32using namespace android::hardware::sensors::V1_0::implementation;
Mathias Agopianf001c922010-11-11 17:58:51 -080033
Peng Xu2bec6232016-07-01 17:13:10 -070034
Mathias Agopianf001c922010-11-11 17:58:51 -080035namespace android {
36// ---------------------------------------------------------------------------
Mathias Agopianf001c922010-11-11 17:58:51 -080037
38ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
39
Steven Morelandd15c0302016-12-20 11:14:50 -080040static status_t StatusFromResult(Result result) {
41 switch (result) {
42 case Result::OK:
43 return OK;
44 case Result::BAD_VALUE:
45 return BAD_VALUE;
46 case Result::PERMISSION_DENIED:
47 return PERMISSION_DENIED;
48 case Result::INVALID_OPERATION:
49 return INVALID_OPERATION;
50 case Result::NO_MEMORY:
51 return NO_MEMORY;
Mathias Agopianf001c922010-11-11 17:58:51 -080052 }
53}
54
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070055SensorDevice::SensorDevice() : mHidlTransportErrors(20) {
Peng Xua8fad9b2017-03-17 12:20:27 -070056 if (!connectHidlService()) {
57 return;
58 }
Ashutosh Joshifea2d262017-04-19 23:27:49 -070059
60 float minPowerMa = 0.001; // 1 microAmp
61
Peng Xua8fad9b2017-03-17 12:20:27 -070062 checkReturn(mSensors->getSensorsList(
63 [&](const auto &list) {
64 const size_t count = list.size();
65
66 mActivationCount.setCapacity(count);
67 Info model;
68 for (size_t i=0 ; i < count; i++) {
69 sensor_t sensor;
70 convertToSensor(list[i], &sensor);
Ashutosh Joshifea2d262017-04-19 23:27:49 -070071 // Sanity check and clamp power if it is 0 (or close)
72 if (sensor.power < minPowerMa) {
73 ALOGE("Reported power %f not deemed sane, clamping to %f",
74 sensor.power, minPowerMa);
75 sensor.power = minPowerMa;
76 }
Peng Xua8fad9b2017-03-17 12:20:27 -070077 mSensorList.push_back(sensor);
78
79 mActivationCount.add(list[i].sensorHandle, model);
80
81 checkReturn(mSensors->activate(list[i].sensorHandle, 0 /* enabled */));
82 }
83 }));
84
85 mIsDirectReportSupported =
86 (checkReturn(mSensors->unregisterDirectChannel(-1)) != Result::INVALID_OPERATION);
87}
88
89bool SensorDevice::connectHidlService() {
Peng Xu3889e6e2017-03-02 19:10:38 -080090 // SensorDevice may wait upto 100ms * 10 = 1s for hidl service.
91 constexpr auto RETRY_DELAY = std::chrono::milliseconds(100);
92 size_t retry = 10;
Steven Morelandd15c0302016-12-20 11:14:50 -080093
Peng Xu3889e6e2017-03-02 19:10:38 -080094 while (true) {
95 int initStep = 0;
96 mSensors = ISensors::getService();
97 if (mSensors != nullptr) {
98 ++initStep;
99 // Poke ISensor service. If it has lingering connection from previous generation of
100 // system server, it will kill itself. There is no intention to handle the poll result,
101 // which will be done since the size is 0.
102 if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) {
103 // ok to continue
104 break;
105 }
106 // hidl service is restarting, pointer is invalid.
107 mSensors = nullptr;
108 }
109
110 if (--retry <= 0) {
111 ALOGE("Cannot connect to ISensors hidl service!");
Peng Xua8fad9b2017-03-17 12:20:27 -0700112 break;
Peng Xu3889e6e2017-03-02 19:10:38 -0800113 }
114 // Delay 100ms before retry, hidl service is expected to come up in short time after
115 // crash.
116 ALOGI("%s unsuccessful, try again soon (remaining retry %zu).",
117 (initStep == 0) ? "getService()" : "poll() check", retry);
118 std::this_thread::sleep_for(RETRY_DELAY);
Steven Morelandd15c0302016-12-20 11:14:50 -0800119 }
Peng Xua8fad9b2017-03-17 12:20:27 -0700120 return (mSensors != nullptr);
Steven Morelandd15c0302016-12-20 11:14:50 -0800121}
122
Peng Xu2576cb62016-01-20 00:22:09 -0800123void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700124 // not need to check mSensors because this is is only called after successful poll()
Peng Xu2576cb62016-01-20 00:22:09 -0800125 if (connected) {
126 Info model;
127 mActivationCount.add(handle, model);
Peng Xu3889e6e2017-03-02 19:10:38 -0800128 checkReturn(mSensors->activate(handle, 0 /* enabled */));
Peng Xu2576cb62016-01-20 00:22:09 -0800129 } else {
130 mActivationCount.removeItem(handle);
131 }
132}
133
Peng Xu6a2d3a02015-12-21 12:00:23 -0800134std::string SensorDevice::dump() const {
Peng Xua8fad9b2017-03-17 12:20:27 -0700135 if (mSensors == nullptr) return "HAL not initialized\n";
Mathias Agopianf001c922010-11-11 17:58:51 -0800136
Peng Xu6a2d3a02015-12-21 12:00:23 -0800137 String8 result;
Peng Xua8fad9b2017-03-17 12:20:27 -0700138 result.appendFormat("Total %zu h/w sensors, %zu running:\n",
139 mSensorList.size(), mActivationCount.size());
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700140
Peng Xua8fad9b2017-03-17 12:20:27 -0700141 Mutex::Autolock _l(mLock);
142 for (const auto & s : mSensorList) {
143 int32_t handle = s.handle;
144 const Info& info = mActivationCount.valueFor(handle);
145 if (info.batchParams.isEmpty()) continue;
146
147 result.appendFormat("0x%08x) active-count = %zu; ", handle, info.batchParams.size());
148
149 result.append("sampling_period(ms) = {");
150 for (size_t j = 0; j < info.batchParams.size(); j++) {
Peng Xu2bec6232016-07-01 17:13:10 -0700151 const BatchParams& params = info.batchParams[j];
152 result.appendFormat("%.1f%s", params.mTSample / 1e6f,
Peng Xua8fad9b2017-03-17 12:20:27 -0700153 j < info.batchParams.size() - 1 ? ", " : "");
154 }
Peng Xu2bec6232016-07-01 17:13:10 -0700155 result.appendFormat("}, selected = %.2f ms; ", info.bestBatchParams.mTSample / 1e6f);
Peng Xua8fad9b2017-03-17 12:20:27 -0700156
157 result.append("batching_period(ms) = {");
158 for (size_t j = 0; j < info.batchParams.size(); j++) {
Peng Xu2bec6232016-07-01 17:13:10 -0700159 const BatchParams& params = info.batchParams[j];
160 result.appendFormat("%.1f%s", params.mTBatch / 1e6f,
Peng Xua8fad9b2017-03-17 12:20:27 -0700161 j < info.batchParams.size() - 1 ? ", " : "");
162 }
Peng Xu2bec6232016-07-01 17:13:10 -0700163 result.appendFormat("}, selected = %.2f ms\n", info.bestBatchParams.mTBatch / 1e6f);
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700164 }
165
Peng Xu6a2d3a02015-12-21 12:00:23 -0800166 return result.string();
Mathias Agopianf001c922010-11-11 17:58:51 -0800167}
168
169ssize_t SensorDevice::getSensorList(sensor_t const** list) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800170 *list = &mSensorList[0];
171
172 return mSensorList.size();
Mathias Agopianf001c922010-11-11 17:58:51 -0800173}
174
175status_t SensorDevice::initCheck() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800176 return mSensors != NULL ? NO_ERROR : NO_INIT;
Mathias Agopianf001c922010-11-11 17:58:51 -0800177}
178
179ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700180 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800181
182 ssize_t err;
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700183 int numHidlTransportErrors = 0;
184 bool hidlTransportError = false;
Steven Morelandd15c0302016-12-20 11:14:50 -0800185
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700186 do {
187 auto ret = mSensors->poll(
188 count,
189 [&](auto result,
190 const auto &events,
191 const auto &dynamicSensorsAdded) {
192 if (result == Result::OK) {
193 convertToSensorEvents(events, dynamicSensorsAdded, buffer);
194 err = (ssize_t)events.size();
195 } else {
196 err = StatusFromResult(result);
197 }
198 });
199
200 if (ret.isOk()) {
201 hidlTransportError = false;
202 } else {
203 hidlTransportError = true;
204 numHidlTransportErrors++;
205 if (numHidlTransportErrors > 50) {
206 // Log error and bail
207 ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors);
208 handleHidlDeath(ret.description());
209 } else {
210 std::this_thread::sleep_for(std::chrono::milliseconds(10));
211 }
212 }
213 } while (hidlTransportError);
214
215 if(numHidlTransportErrors > 0) {
216 ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors);
217 HidlTransportErrorLog errLog(time(NULL), numHidlTransportErrors);
218 mHidlTransportErrors.add(errLog);
219 mTotalHidlTransportErrors++;
220 }
Steven Morelandd15c0302016-12-20 11:14:50 -0800221
222 return err;
Mathias Agopianf001c922010-11-11 17:58:51 -0800223}
224
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700225void SensorDevice::autoDisable(void *ident, int handle) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700226 Mutex::Autolock _l(mLock);
Peng Xu042baec2017-08-09 19:28:27 -0700227 ssize_t activationIndex = mActivationCount.indexOfKey(handle);
228 if (activationIndex < 0) {
229 ALOGW("Handle %d cannot be found in activation record", handle);
230 return;
231 }
232 Info& info(mActivationCount.editValueAt(activationIndex));
Aravind Akella724d91d2013-06-27 12:04:23 -0700233 info.removeBatchParamsForIdent(ident);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700234}
235
Peng Xu6a2d3a02015-12-21 12:00:23 -0800236status_t SensorDevice::activate(void* ident, int handle, int enabled) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700237 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800238
Mathias Agopianf001c922010-11-11 17:58:51 -0800239 status_t err(NO_ERROR);
240 bool actuateHardware = false;
241
Aravind Akella724d91d2013-06-27 12:04:23 -0700242 Mutex::Autolock _l(mLock);
Peng Xu042baec2017-08-09 19:28:27 -0700243 ssize_t activationIndex = mActivationCount.indexOfKey(handle);
244 if (activationIndex < 0) {
245 ALOGW("Handle %d cannot be found in activation record", handle);
246 return BAD_VALUE;
247 }
248 Info& info(mActivationCount.editValueAt(activationIndex));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700249
Steve Blocka5512372011-12-20 16:23:08 +0000250 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700251 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
Aravind Akella724d91d2013-06-27 12:04:23 -0700252 ident, handle, enabled, info.batchParams.size());
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700253
Mathias Agopianf001c922010-11-11 17:58:51 -0800254 if (enabled) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700255 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700256
Aravind Akella4949c502015-02-11 15:54:35 -0800257 if (isClientDisabledLocked(ident)) {
Peng Xu966fa882016-09-01 16:13:15 -0700258 ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d",
259 ident, handle);
Aravind Akella4949c502015-02-11 15:54:35 -0800260 return INVALID_OPERATION;
261 }
262
Aravind Akella724d91d2013-06-27 12:04:23 -0700263 if (info.batchParams.indexOfKey(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800264 if (info.numActiveClients() == 1) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700265 // This is the first connection, we need to activate the underlying h/w sensor.
266 actuateHardware = true;
267 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800268 } else {
Aravind Akella724d91d2013-06-27 12:04:23 -0700269 // Log error. Every activate call should be preceded by a batch() call.
270 ALOGE("\t >>>ERROR: activate called without batch");
Mathias Agopianf001c922010-11-11 17:58:51 -0800271 }
272 } else {
Mark Salyzyndb458612014-06-10 14:50:02 -0700273 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700274
Steven Morelandd15c0302016-12-20 11:14:50 -0800275 // If a connected dynamic sensor is deactivated, remove it from the
276 // dictionary.
277 auto it = mConnectedDynamicSensors.find(handle);
278 if (it != mConnectedDynamicSensors.end()) {
279 delete it->second;
280 mConnectedDynamicSensors.erase(it);
281 }
282
Aravind Akella724d91d2013-06-27 12:04:23 -0700283 if (info.removeBatchParamsForIdent(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800284 if (info.numActiveClients() == 0) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700285 // This is the last connection, we need to de-activate the underlying h/w sensor.
Mathias Agopian50b66762010-11-29 17:26:51 -0800286 actuateHardware = true;
Aravind Akella724d91d2013-06-27 12:04:23 -0700287 } else {
Steven Morelandd15c0302016-12-20 11:14:50 -0800288 // Call batch for this sensor with the previously calculated best effort
289 // batch_rate and timeout. One of the apps has unregistered for sensor
290 // events, and the best effort batch parameters might have changed.
291 ALOGD_IF(DEBUG_CONNECTIONS,
Peng Xu2bec6232016-07-01 17:13:10 -0700292 "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle,
293 info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch);
Peng Xu3889e6e2017-03-02 19:10:38 -0800294 checkReturn(mSensors->batch(
Peng Xu2bec6232016-07-01 17:13:10 -0700295 handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch));
Mathias Agopian50b66762010-11-29 17:26:51 -0800296 }
297 } else {
298 // sensor wasn't enabled for this ident
299 }
Aravind Akella4949c502015-02-11 15:54:35 -0800300
301 if (isClientDisabledLocked(ident)) {
302 return NO_ERROR;
303 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800304 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800305
Mathias Agopianf001c922010-11-11 17:58:51 -0800306 if (actuateHardware) {
Aravind Akella4949c502015-02-11 15:54:35 -0800307 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
308 enabled);
Peng Xu3889e6e2017-03-02 19:10:38 -0800309 err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700310 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
311 strerror(-err));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700312
Aravind Akella724d91d2013-06-27 12:04:23 -0700313 if (err != NO_ERROR && enabled) {
314 // Failure when enabling the sensor. Clean up on failure.
315 info.removeBatchParamsForIdent(ident);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700316 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800317 }
318
Mathias Agopianf001c922010-11-11 17:58:51 -0800319 return err;
320}
321
Steven Morelandd15c0302016-12-20 11:14:50 -0800322status_t SensorDevice::batch(
323 void* ident,
324 int handle,
325 int flags,
326 int64_t samplingPeriodNs,
327 int64_t maxBatchReportLatencyNs) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700328 if (mSensors == nullptr) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700329
330 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
331 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
332 }
Peng Xu2bec6232016-07-01 17:13:10 -0700333 if (maxBatchReportLatencyNs < 0) {
334 maxBatchReportLatencyNs = 0;
335 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700336
Aravind Akella724d91d2013-06-27 12:04:23 -0700337 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700338 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700339 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
340
341 Mutex::Autolock _l(mLock);
Peng Xu042baec2017-08-09 19:28:27 -0700342 ssize_t activationIndex = mActivationCount.indexOfKey(handle);
343 if (activationIndex < 0) {
344 ALOGW("Handle %d cannot be found in activation record", handle);
345 return BAD_VALUE;
346 }
347 Info& info(mActivationCount.editValueAt(activationIndex));
Aravind Akella724d91d2013-06-27 12:04:23 -0700348
349 if (info.batchParams.indexOfKey(ident) < 0) {
Peng Xu2bec6232016-07-01 17:13:10 -0700350 BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs);
Aravind Akella724d91d2013-06-27 12:04:23 -0700351 info.batchParams.add(ident, params);
352 } else {
353 // A batch has already been called with this ident. Update the batch parameters.
354 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
355 }
356
357 BatchParams prevBestBatchParams = info.bestBatchParams;
358 // Find the minimum of all timeouts and batch_rates for this sensor.
359 info.selectBatchParams();
360
361 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700362 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
363 " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
Peng Xu2bec6232016-07-01 17:13:10 -0700364 prevBestBatchParams.mTSample, info.bestBatchParams.mTSample,
365 prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch);
Aravind Akella724d91d2013-06-27 12:04:23 -0700366
367 status_t err(NO_ERROR);
368 // If the min period or min timeout has changed since the last batch call, call batch.
369 if (prevBestBatchParams != info.bestBatchParams) {
Peng Xu2bec6232016-07-01 17:13:10 -0700370 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle,
371 info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch);
Steven Morelandd15c0302016-12-20 11:14:50 -0800372 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800373 checkReturn(mSensors->batch(
Peng Xu2bec6232016-07-01 17:13:10 -0700374 handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700375 if (err != NO_ERROR) {
Peng Xu2bec6232016-07-01 17:13:10 -0700376 ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s",
377 mSensors.get(), handle, info.bestBatchParams.mTSample,
378 info.bestBatchParams.mTBatch, strerror(-err));
Aravind Akella724d91d2013-06-27 12:04:23 -0700379 info.removeBatchParamsForIdent(ident);
380 }
381 }
382 return err;
383}
384
Peng Xu6a2d3a02015-12-21 12:00:23 -0800385status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) {
Peng Xu2bec6232016-07-01 17:13:10 -0700386 return batch(ident, handle, 0, samplingPeriodNs, 0);
Mathias Agopian667102f2011-09-14 16:43:34 -0700387}
388
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700389int SensorDevice::getHalDeviceVersion() const {
Peng Xua8fad9b2017-03-17 12:20:27 -0700390 if (mSensors == nullptr) return -1;
Steven Morelandd15c0302016-12-20 11:14:50 -0800391 return SENSORS_DEVICE_API_VERSION_1_4;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700392}
393
Aravind Akella9a844cf2014-02-11 18:58:52 -0800394status_t SensorDevice::flush(void* ident, int handle) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700395 if (mSensors == nullptr) return NO_INIT;
Aravind Akella4949c502015-02-11 15:54:35 -0800396 if (isClientDisabled(ident)) return INVALID_OPERATION;
Aravind Akella724d91d2013-06-27 12:04:23 -0700397 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800398 return StatusFromResult(checkReturn(mSensors->flush(handle)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700399}
400
Aravind Akella4949c502015-02-11 15:54:35 -0800401bool SensorDevice::isClientDisabled(void* ident) {
402 Mutex::Autolock _l(mLock);
403 return isClientDisabledLocked(ident);
404}
405
406bool SensorDevice::isClientDisabledLocked(void* ident) {
407 return mDisabledClients.indexOf(ident) >= 0;
408}
409
410void SensorDevice::enableAllSensors() {
Peng Xua8fad9b2017-03-17 12:20:27 -0700411 if (mSensors == nullptr) return;
Aravind Akella4949c502015-02-11 15:54:35 -0800412 Mutex::Autolock _l(mLock);
413 mDisabledClients.clear();
Peng Xu966fa882016-09-01 16:13:15 -0700414 ALOGI("cleared mDisabledClients");
Aravind Akella4949c502015-02-11 15:54:35 -0800415 for (size_t i = 0; i< mActivationCount.size(); ++i) {
416 Info& info = mActivationCount.editValueAt(i);
417 if (info.batchParams.isEmpty()) continue;
418 info.selectBatchParams();
419 const int sensor_handle = mActivationCount.keyAt(i);
420 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ",
421 sensor_handle);
Steven Morelandd15c0302016-12-20 11:14:50 -0800422 status_t err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800423 checkReturn(mSensors->batch(
Steven Morelandd15c0302016-12-20 11:14:50 -0800424 sensor_handle,
Peng Xu2bec6232016-07-01 17:13:10 -0700425 info.bestBatchParams.mTSample,
426 info.bestBatchParams.mTBatch)));
Steven Morelandd15c0302016-12-20 11:14:50 -0800427 ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err));
Aravind Akella4949c502015-02-11 15:54:35 -0800428
429 if (err == NO_ERROR) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800430 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800431 checkReturn(mSensors->activate(sensor_handle, 1 /* enabled */)));
Aravind Akella4949c502015-02-11 15:54:35 -0800432 ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err));
433 }
Aravind Akella4949c502015-02-11 15:54:35 -0800434 }
435}
436
437void SensorDevice::disableAllSensors() {
Peng Xua8fad9b2017-03-17 12:20:27 -0700438 if (mSensors == nullptr) return;
Aravind Akella4949c502015-02-11 15:54:35 -0800439 Mutex::Autolock _l(mLock);
Peng Xua8fad9b2017-03-17 12:20:27 -0700440 for (size_t i = 0; i< mActivationCount.size(); ++i) {
Aravind Akella4949c502015-02-11 15:54:35 -0800441 const Info& info = mActivationCount.valueAt(i);
442 // Check if this sensor has been activated previously and disable it.
443 if (info.batchParams.size() > 0) {
444 const int sensor_handle = mActivationCount.keyAt(i);
445 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
446 sensor_handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800447 checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */));
Steven Morelandd15c0302016-12-20 11:14:50 -0800448
Aravind Akella4949c502015-02-11 15:54:35 -0800449 // Add all the connections that were registered for this sensor to the disabled
450 // clients list.
Svetoslavb412f6e2015-04-29 16:50:41 -0700451 for (size_t j = 0; j < info.batchParams.size(); ++j) {
Aravind Akella4949c502015-02-11 15:54:35 -0800452 mDisabledClients.add(info.batchParams.keyAt(j));
Peng Xu966fa882016-09-01 16:13:15 -0700453 ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j));
Aravind Akella4949c502015-02-11 15:54:35 -0800454 }
455 }
456 }
457}
458
Steven Morelandd15c0302016-12-20 11:14:50 -0800459status_t SensorDevice::injectSensorData(
460 const sensors_event_t *injected_sensor_event) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700461 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800462 ALOGD_IF(DEBUG_CONNECTIONS,
463 "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
464 injected_sensor_event->sensor,
465 injected_sensor_event->timestamp, injected_sensor_event->data[0],
466 injected_sensor_event->data[1], injected_sensor_event->data[2],
467 injected_sensor_event->data[3], injected_sensor_event->data[4],
468 injected_sensor_event->data[5]);
469
470 Event ev;
471 convertFromSensorEvent(*injected_sensor_event, &ev);
472
Peng Xu3889e6e2017-03-02 19:10:38 -0800473 return StatusFromResult(checkReturn(mSensors->injectSensorData(ev)));
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700474}
475
476status_t SensorDevice::setMode(uint32_t mode) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700477 if (mSensors == nullptr) return NO_INIT;
478 return StatusFromResult(
479 checkReturn(mSensors->setOperationMode(
480 static_cast<hardware::sensors::V1_0::OperationMode>(mode))));
481}
Steven Morelandd15c0302016-12-20 11:14:50 -0800482
Peng Xua8fad9b2017-03-17 12:20:27 -0700483int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) {
484 if (mSensors == nullptr) return NO_INIT;
485 Mutex::Autolock _l(mLock);
486
487 SharedMemType type;
488 switch (memory->type) {
489 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
490 type = SharedMemType::ASHMEM;
491 break;
492 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
493 type = SharedMemType::GRALLOC;
494 break;
495 default:
496 return BAD_VALUE;
497 }
498
499 SharedMemFormat format;
500 if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) {
501 return BAD_VALUE;
502 }
503 format = SharedMemFormat::SENSORS_EVENT;
504
505 SharedMemInfo mem = {
506 .type = type,
507 .format = format,
508 .size = static_cast<uint32_t>(memory->size),
509 .memoryHandle = memory->handle,
510 };
511
512 int32_t ret;
513 checkReturn(mSensors->registerDirectChannel(mem,
514 [&ret](auto result, auto channelHandle) {
515 if (result == Result::OK) {
516 ret = channelHandle;
517 } else {
518 ret = StatusFromResult(result);
519 }
520 }));
521 return ret;
522}
523
524void SensorDevice::unregisterDirectChannel(int32_t channelHandle) {
525 if (mSensors == nullptr) return;
526 Mutex::Autolock _l(mLock);
527 checkReturn(mSensors->unregisterDirectChannel(channelHandle));
528}
529
530int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle,
531 int32_t channelHandle, const struct sensors_direct_cfg_t *config) {
532 if (mSensors == nullptr) return NO_INIT;
533 Mutex::Autolock _l(mLock);
534
535 RateLevel rate;
536 switch(config->rate_level) {
537 case SENSOR_DIRECT_RATE_STOP:
538 rate = RateLevel::STOP;
539 break;
540 case SENSOR_DIRECT_RATE_NORMAL:
541 rate = RateLevel::NORMAL;
542 break;
543 case SENSOR_DIRECT_RATE_FAST:
544 rate = RateLevel::FAST;
545 break;
546 case SENSOR_DIRECT_RATE_VERY_FAST:
547 rate = RateLevel::VERY_FAST;
548 break;
549 default:
550 return BAD_VALUE;
551 }
552
553 int32_t ret;
554 checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate,
555 [&ret, rate] (auto result, auto token) {
556 if (rate == RateLevel::STOP) {
557 ret = StatusFromResult(result);
558 } else {
559 if (result == Result::OK) {
560 ret = token;
561 } else {
562 ret = StatusFromResult(result);
563 }
564 }
565 }));
566
567 return ret;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700568}
569
Mathias Agopian667102f2011-09-14 16:43:34 -0700570// ---------------------------------------------------------------------------
571
Aravind Akella4949c502015-02-11 15:54:35 -0800572int SensorDevice::Info::numActiveClients() {
573 SensorDevice& device(SensorDevice::getInstance());
574 int num = 0;
575 for (size_t i = 0; i < batchParams.size(); ++i) {
576 if (!device.isClientDisabledLocked(batchParams.keyAt(i))) {
577 ++num;
578 }
579 }
580 return num;
581}
582
Peng Xu2bec6232016-07-01 17:13:10 -0700583status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int,
Aravind Akella724d91d2013-06-27 12:04:23 -0700584 int64_t samplingPeriodNs,
585 int64_t maxBatchReportLatencyNs) {
586 ssize_t index = batchParams.indexOfKey(ident);
Mathias Agopian667102f2011-09-14 16:43:34 -0700587 if (index < 0) {
Peng Xu2bec6232016-07-01 17:13:10 -0700588 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64
589 " timeout=%" PRId64 ") failed (%s)",
Aravind Akella724d91d2013-06-27 12:04:23 -0700590 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
Mathias Agopian667102f2011-09-14 16:43:34 -0700591 return BAD_INDEX;
592 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700593 BatchParams& params = batchParams.editValueAt(index);
Peng Xu2bec6232016-07-01 17:13:10 -0700594 params.mTSample = samplingPeriodNs;
595 params.mTBatch = maxBatchReportLatencyNs;
Mathias Agopian667102f2011-09-14 16:43:34 -0700596 return NO_ERROR;
597}
598
Aravind Akella724d91d2013-06-27 12:04:23 -0700599void SensorDevice::Info::selectBatchParams() {
Peng Xu2bec6232016-07-01 17:13:10 -0700600 BatchParams bestParams; // default to max Tsample and max Tbatch
Aravind Akella4949c502015-02-11 15:54:35 -0800601 SensorDevice& device(SensorDevice::getInstance());
Aravind Akella724d91d2013-06-27 12:04:23 -0700602
Aravind Akella4949c502015-02-11 15:54:35 -0800603 for (size_t i = 0; i < batchParams.size(); ++i) {
Peng Xu2bec6232016-07-01 17:13:10 -0700604 if (device.isClientDisabledLocked(batchParams.keyAt(i))) {
605 continue;
Aravind Akella724d91d2013-06-27 12:04:23 -0700606 }
Peng Xu2bec6232016-07-01 17:13:10 -0700607 bestParams.merge(batchParams[i]);
608 }
609 // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly.
610 if (bestParams.mTBatch <= bestParams.mTSample) {
611 bestParams.mTBatch = 0;
Mathias Agopianf001c922010-11-11 17:58:51 -0800612 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700613 bestBatchParams = bestParams;
614}
615
616ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
617 ssize_t idx = batchParams.removeItem(ident);
618 if (idx >= 0) {
619 selectBatchParams();
620 }
621 return idx;
Mathias Agopianf001c922010-11-11 17:58:51 -0800622}
623
Peng Xu4f707f82016-09-26 11:28:32 -0700624void SensorDevice::notifyConnectionDestroyed(void* ident) {
625 Mutex::Autolock _l(mLock);
626 mDisabledClients.remove(ident);
627}
628
Peng Xu53632542017-01-23 20:06:27 -0800629bool SensorDevice::isDirectReportSupported() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800630 return mIsDirectReportSupported;
Peng Xu53632542017-01-23 20:06:27 -0800631}
Steven Morelandd15c0302016-12-20 11:14:50 -0800632
633void SensorDevice::convertToSensorEvent(
634 const Event &src, sensors_event_t *dst) {
635 ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent(
636 src, dst);
637
638 if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) {
639 const DynamicSensorInfo &dyn = src.u.dynamic;
640
641 dst->dynamic_sensor_meta.connected = dyn.connected;
642 dst->dynamic_sensor_meta.handle = dyn.sensorHandle;
643 if (dyn.connected) {
644 auto it = mConnectedDynamicSensors.find(dyn.sensorHandle);
645 CHECK(it != mConnectedDynamicSensors.end());
646
647 dst->dynamic_sensor_meta.sensor = it->second;
648
649 memcpy(dst->dynamic_sensor_meta.uuid,
650 dyn.uuid.data(),
651 sizeof(dst->dynamic_sensor_meta.uuid));
652 }
653 }
654}
655
656void SensorDevice::convertToSensorEvents(
657 const hidl_vec<Event> &src,
658 const hidl_vec<SensorInfo> &dynamicSensorsAdded,
659 sensors_event_t *dst) {
660 // Allocate a sensor_t structure for each dynamic sensor added and insert
661 // it into the dictionary of connected dynamic sensors keyed by handle.
662 for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) {
663 const SensorInfo &info = dynamicSensorsAdded[i];
664
665 auto it = mConnectedDynamicSensors.find(info.sensorHandle);
666 CHECK(it == mConnectedDynamicSensors.end());
667
668 sensor_t *sensor = new sensor_t;
669 convertToSensor(info, sensor);
670
671 mConnectedDynamicSensors.insert(
672 std::make_pair(sensor->handle, sensor));
673 }
674
675 for (size_t i = 0; i < src.size(); ++i) {
676 convertToSensorEvent(src[i], &dst[i]);
677 }
678}
679
Peng Xu3889e6e2017-03-02 19:10:38 -0800680void SensorDevice::handleHidlDeath(const std::string & detail) {
681 // restart is the only option at present.
682 LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str());
683}
684
Mathias Agopianf001c922010-11-11 17:58:51 -0800685// ---------------------------------------------------------------------------
686}; // namespace android