blob: 7d9b0b730a34a4e7d92e612614c4b8c87a1390f3 [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) {
226 Info& info( mActivationCount.editValueFor(handle) );
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700227 Mutex::Autolock _l(mLock);
Aravind Akella724d91d2013-06-27 12:04:23 -0700228 info.removeBatchParamsForIdent(ident);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700229}
230
Peng Xu6a2d3a02015-12-21 12:00:23 -0800231status_t SensorDevice::activate(void* ident, int handle, int enabled) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700232 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800233
Mathias Agopianf001c922010-11-11 17:58:51 -0800234 status_t err(NO_ERROR);
235 bool actuateHardware = false;
236
Aravind Akella724d91d2013-06-27 12:04:23 -0700237 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800238 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700239
Steve Blocka5512372011-12-20 16:23:08 +0000240 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700241 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
Aravind Akella724d91d2013-06-27 12:04:23 -0700242 ident, handle, enabled, info.batchParams.size());
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700243
Mathias Agopianf001c922010-11-11 17:58:51 -0800244 if (enabled) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700245 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700246
Aravind Akella4949c502015-02-11 15:54:35 -0800247 if (isClientDisabledLocked(ident)) {
Peng Xu966fa882016-09-01 16:13:15 -0700248 ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d",
249 ident, handle);
Aravind Akella4949c502015-02-11 15:54:35 -0800250 return INVALID_OPERATION;
251 }
252
Aravind Akella724d91d2013-06-27 12:04:23 -0700253 if (info.batchParams.indexOfKey(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800254 if (info.numActiveClients() == 1) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700255 // This is the first connection, we need to activate the underlying h/w sensor.
256 actuateHardware = true;
257 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800258 } else {
Aravind Akella724d91d2013-06-27 12:04:23 -0700259 // Log error. Every activate call should be preceded by a batch() call.
260 ALOGE("\t >>>ERROR: activate called without batch");
Mathias Agopianf001c922010-11-11 17:58:51 -0800261 }
262 } else {
Mark Salyzyndb458612014-06-10 14:50:02 -0700263 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700264
Steven Morelandd15c0302016-12-20 11:14:50 -0800265 // If a connected dynamic sensor is deactivated, remove it from the
266 // dictionary.
267 auto it = mConnectedDynamicSensors.find(handle);
268 if (it != mConnectedDynamicSensors.end()) {
269 delete it->second;
270 mConnectedDynamicSensors.erase(it);
271 }
272
Aravind Akella724d91d2013-06-27 12:04:23 -0700273 if (info.removeBatchParamsForIdent(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800274 if (info.numActiveClients() == 0) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700275 // This is the last connection, we need to de-activate the underlying h/w sensor.
Mathias Agopian50b66762010-11-29 17:26:51 -0800276 actuateHardware = true;
Aravind Akella724d91d2013-06-27 12:04:23 -0700277 } else {
Steven Morelandd15c0302016-12-20 11:14:50 -0800278 // Call batch for this sensor with the previously calculated best effort
279 // batch_rate and timeout. One of the apps has unregistered for sensor
280 // events, and the best effort batch parameters might have changed.
281 ALOGD_IF(DEBUG_CONNECTIONS,
Peng Xu2bec6232016-07-01 17:13:10 -0700282 "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle,
283 info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch);
Peng Xu3889e6e2017-03-02 19:10:38 -0800284 checkReturn(mSensors->batch(
Peng Xu2bec6232016-07-01 17:13:10 -0700285 handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch));
Mathias Agopian50b66762010-11-29 17:26:51 -0800286 }
287 } else {
288 // sensor wasn't enabled for this ident
289 }
Aravind Akella4949c502015-02-11 15:54:35 -0800290
291 if (isClientDisabledLocked(ident)) {
292 return NO_ERROR;
293 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800294 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800295
Mathias Agopianf001c922010-11-11 17:58:51 -0800296 if (actuateHardware) {
Aravind Akella4949c502015-02-11 15:54:35 -0800297 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
298 enabled);
Peng Xu3889e6e2017-03-02 19:10:38 -0800299 err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700300 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
301 strerror(-err));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700302
Aravind Akella724d91d2013-06-27 12:04:23 -0700303 if (err != NO_ERROR && enabled) {
304 // Failure when enabling the sensor. Clean up on failure.
305 info.removeBatchParamsForIdent(ident);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700306 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800307 }
308
Mathias Agopianf001c922010-11-11 17:58:51 -0800309 return err;
310}
311
Steven Morelandd15c0302016-12-20 11:14:50 -0800312status_t SensorDevice::batch(
313 void* ident,
314 int handle,
315 int flags,
316 int64_t samplingPeriodNs,
317 int64_t maxBatchReportLatencyNs) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700318 if (mSensors == nullptr) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700319
320 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
321 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
322 }
Peng Xu2bec6232016-07-01 17:13:10 -0700323 if (maxBatchReportLatencyNs < 0) {
324 maxBatchReportLatencyNs = 0;
325 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700326
Aravind Akella724d91d2013-06-27 12:04:23 -0700327 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700328 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700329 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
330
331 Mutex::Autolock _l(mLock);
332 Info& info(mActivationCount.editValueFor(handle));
333
334 if (info.batchParams.indexOfKey(ident) < 0) {
Peng Xu2bec6232016-07-01 17:13:10 -0700335 BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs);
Aravind Akella724d91d2013-06-27 12:04:23 -0700336 info.batchParams.add(ident, params);
337 } else {
338 // A batch has already been called with this ident. Update the batch parameters.
339 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
340 }
341
342 BatchParams prevBestBatchParams = info.bestBatchParams;
343 // Find the minimum of all timeouts and batch_rates for this sensor.
344 info.selectBatchParams();
345
346 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700347 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
348 " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
Peng Xu2bec6232016-07-01 17:13:10 -0700349 prevBestBatchParams.mTSample, info.bestBatchParams.mTSample,
350 prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch);
Aravind Akella724d91d2013-06-27 12:04:23 -0700351
352 status_t err(NO_ERROR);
353 // If the min period or min timeout has changed since the last batch call, call batch.
354 if (prevBestBatchParams != info.bestBatchParams) {
Peng Xu2bec6232016-07-01 17:13:10 -0700355 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle,
356 info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch);
Steven Morelandd15c0302016-12-20 11:14:50 -0800357 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800358 checkReturn(mSensors->batch(
Peng Xu2bec6232016-07-01 17:13:10 -0700359 handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700360 if (err != NO_ERROR) {
Peng Xu2bec6232016-07-01 17:13:10 -0700361 ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s",
362 mSensors.get(), handle, info.bestBatchParams.mTSample,
363 info.bestBatchParams.mTBatch, strerror(-err));
Aravind Akella724d91d2013-06-27 12:04:23 -0700364 info.removeBatchParamsForIdent(ident);
365 }
366 }
367 return err;
368}
369
Peng Xu6a2d3a02015-12-21 12:00:23 -0800370status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) {
Peng Xu2bec6232016-07-01 17:13:10 -0700371 return batch(ident, handle, 0, samplingPeriodNs, 0);
Mathias Agopian667102f2011-09-14 16:43:34 -0700372}
373
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700374int SensorDevice::getHalDeviceVersion() const {
Peng Xua8fad9b2017-03-17 12:20:27 -0700375 if (mSensors == nullptr) return -1;
Steven Morelandd15c0302016-12-20 11:14:50 -0800376 return SENSORS_DEVICE_API_VERSION_1_4;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700377}
378
Aravind Akella9a844cf2014-02-11 18:58:52 -0800379status_t SensorDevice::flush(void* ident, int handle) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700380 if (mSensors == nullptr) return NO_INIT;
Aravind Akella4949c502015-02-11 15:54:35 -0800381 if (isClientDisabled(ident)) return INVALID_OPERATION;
Aravind Akella724d91d2013-06-27 12:04:23 -0700382 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800383 return StatusFromResult(checkReturn(mSensors->flush(handle)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700384}
385
Aravind Akella4949c502015-02-11 15:54:35 -0800386bool SensorDevice::isClientDisabled(void* ident) {
387 Mutex::Autolock _l(mLock);
388 return isClientDisabledLocked(ident);
389}
390
391bool SensorDevice::isClientDisabledLocked(void* ident) {
392 return mDisabledClients.indexOf(ident) >= 0;
393}
394
395void SensorDevice::enableAllSensors() {
Peng Xua8fad9b2017-03-17 12:20:27 -0700396 if (mSensors == nullptr) return;
Aravind Akella4949c502015-02-11 15:54:35 -0800397 Mutex::Autolock _l(mLock);
398 mDisabledClients.clear();
Peng Xu966fa882016-09-01 16:13:15 -0700399 ALOGI("cleared mDisabledClients");
Aravind Akella4949c502015-02-11 15:54:35 -0800400 for (size_t i = 0; i< mActivationCount.size(); ++i) {
401 Info& info = mActivationCount.editValueAt(i);
402 if (info.batchParams.isEmpty()) continue;
403 info.selectBatchParams();
404 const int sensor_handle = mActivationCount.keyAt(i);
405 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ",
406 sensor_handle);
Steven Morelandd15c0302016-12-20 11:14:50 -0800407 status_t err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800408 checkReturn(mSensors->batch(
Steven Morelandd15c0302016-12-20 11:14:50 -0800409 sensor_handle,
Peng Xu2bec6232016-07-01 17:13:10 -0700410 info.bestBatchParams.mTSample,
411 info.bestBatchParams.mTBatch)));
Steven Morelandd15c0302016-12-20 11:14:50 -0800412 ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err));
Aravind Akella4949c502015-02-11 15:54:35 -0800413
414 if (err == NO_ERROR) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800415 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800416 checkReturn(mSensors->activate(sensor_handle, 1 /* enabled */)));
Aravind Akella4949c502015-02-11 15:54:35 -0800417 ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err));
418 }
Aravind Akella4949c502015-02-11 15:54:35 -0800419 }
420}
421
422void SensorDevice::disableAllSensors() {
Peng Xua8fad9b2017-03-17 12:20:27 -0700423 if (mSensors == nullptr) return;
Aravind Akella4949c502015-02-11 15:54:35 -0800424 Mutex::Autolock _l(mLock);
Peng Xua8fad9b2017-03-17 12:20:27 -0700425 for (size_t i = 0; i< mActivationCount.size(); ++i) {
Aravind Akella4949c502015-02-11 15:54:35 -0800426 const Info& info = mActivationCount.valueAt(i);
427 // Check if this sensor has been activated previously and disable it.
428 if (info.batchParams.size() > 0) {
429 const int sensor_handle = mActivationCount.keyAt(i);
430 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
431 sensor_handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800432 checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */));
Steven Morelandd15c0302016-12-20 11:14:50 -0800433
Aravind Akella4949c502015-02-11 15:54:35 -0800434 // Add all the connections that were registered for this sensor to the disabled
435 // clients list.
Svetoslavb412f6e2015-04-29 16:50:41 -0700436 for (size_t j = 0; j < info.batchParams.size(); ++j) {
Aravind Akella4949c502015-02-11 15:54:35 -0800437 mDisabledClients.add(info.batchParams.keyAt(j));
Peng Xu966fa882016-09-01 16:13:15 -0700438 ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j));
Aravind Akella4949c502015-02-11 15:54:35 -0800439 }
440 }
441 }
442}
443
Steven Morelandd15c0302016-12-20 11:14:50 -0800444status_t SensorDevice::injectSensorData(
445 const sensors_event_t *injected_sensor_event) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700446 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800447 ALOGD_IF(DEBUG_CONNECTIONS,
448 "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
449 injected_sensor_event->sensor,
450 injected_sensor_event->timestamp, injected_sensor_event->data[0],
451 injected_sensor_event->data[1], injected_sensor_event->data[2],
452 injected_sensor_event->data[3], injected_sensor_event->data[4],
453 injected_sensor_event->data[5]);
454
455 Event ev;
456 convertFromSensorEvent(*injected_sensor_event, &ev);
457
Peng Xu3889e6e2017-03-02 19:10:38 -0800458 return StatusFromResult(checkReturn(mSensors->injectSensorData(ev)));
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700459}
460
461status_t SensorDevice::setMode(uint32_t mode) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700462 if (mSensors == nullptr) return NO_INIT;
463 return StatusFromResult(
464 checkReturn(mSensors->setOperationMode(
465 static_cast<hardware::sensors::V1_0::OperationMode>(mode))));
466}
Steven Morelandd15c0302016-12-20 11:14:50 -0800467
Peng Xua8fad9b2017-03-17 12:20:27 -0700468int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) {
469 if (mSensors == nullptr) return NO_INIT;
470 Mutex::Autolock _l(mLock);
471
472 SharedMemType type;
473 switch (memory->type) {
474 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
475 type = SharedMemType::ASHMEM;
476 break;
477 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
478 type = SharedMemType::GRALLOC;
479 break;
480 default:
481 return BAD_VALUE;
482 }
483
484 SharedMemFormat format;
485 if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) {
486 return BAD_VALUE;
487 }
488 format = SharedMemFormat::SENSORS_EVENT;
489
490 SharedMemInfo mem = {
491 .type = type,
492 .format = format,
493 .size = static_cast<uint32_t>(memory->size),
494 .memoryHandle = memory->handle,
495 };
496
497 int32_t ret;
498 checkReturn(mSensors->registerDirectChannel(mem,
499 [&ret](auto result, auto channelHandle) {
500 if (result == Result::OK) {
501 ret = channelHandle;
502 } else {
503 ret = StatusFromResult(result);
504 }
505 }));
506 return ret;
507}
508
509void SensorDevice::unregisterDirectChannel(int32_t channelHandle) {
510 if (mSensors == nullptr) return;
511 Mutex::Autolock _l(mLock);
512 checkReturn(mSensors->unregisterDirectChannel(channelHandle));
513}
514
515int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle,
516 int32_t channelHandle, const struct sensors_direct_cfg_t *config) {
517 if (mSensors == nullptr) return NO_INIT;
518 Mutex::Autolock _l(mLock);
519
520 RateLevel rate;
521 switch(config->rate_level) {
522 case SENSOR_DIRECT_RATE_STOP:
523 rate = RateLevel::STOP;
524 break;
525 case SENSOR_DIRECT_RATE_NORMAL:
526 rate = RateLevel::NORMAL;
527 break;
528 case SENSOR_DIRECT_RATE_FAST:
529 rate = RateLevel::FAST;
530 break;
531 case SENSOR_DIRECT_RATE_VERY_FAST:
532 rate = RateLevel::VERY_FAST;
533 break;
534 default:
535 return BAD_VALUE;
536 }
537
538 int32_t ret;
539 checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate,
540 [&ret, rate] (auto result, auto token) {
541 if (rate == RateLevel::STOP) {
542 ret = StatusFromResult(result);
543 } else {
544 if (result == Result::OK) {
545 ret = token;
546 } else {
547 ret = StatusFromResult(result);
548 }
549 }
550 }));
551
552 return ret;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700553}
554
Mathias Agopian667102f2011-09-14 16:43:34 -0700555// ---------------------------------------------------------------------------
556
Aravind Akella4949c502015-02-11 15:54:35 -0800557int SensorDevice::Info::numActiveClients() {
558 SensorDevice& device(SensorDevice::getInstance());
559 int num = 0;
560 for (size_t i = 0; i < batchParams.size(); ++i) {
561 if (!device.isClientDisabledLocked(batchParams.keyAt(i))) {
562 ++num;
563 }
564 }
565 return num;
566}
567
Peng Xu2bec6232016-07-01 17:13:10 -0700568status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int,
Aravind Akella724d91d2013-06-27 12:04:23 -0700569 int64_t samplingPeriodNs,
570 int64_t maxBatchReportLatencyNs) {
571 ssize_t index = batchParams.indexOfKey(ident);
Mathias Agopian667102f2011-09-14 16:43:34 -0700572 if (index < 0) {
Peng Xu2bec6232016-07-01 17:13:10 -0700573 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64
574 " timeout=%" PRId64 ") failed (%s)",
Aravind Akella724d91d2013-06-27 12:04:23 -0700575 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
Mathias Agopian667102f2011-09-14 16:43:34 -0700576 return BAD_INDEX;
577 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700578 BatchParams& params = batchParams.editValueAt(index);
Peng Xu2bec6232016-07-01 17:13:10 -0700579 params.mTSample = samplingPeriodNs;
580 params.mTBatch = maxBatchReportLatencyNs;
Mathias Agopian667102f2011-09-14 16:43:34 -0700581 return NO_ERROR;
582}
583
Aravind Akella724d91d2013-06-27 12:04:23 -0700584void SensorDevice::Info::selectBatchParams() {
Peng Xu2bec6232016-07-01 17:13:10 -0700585 BatchParams bestParams; // default to max Tsample and max Tbatch
Aravind Akella4949c502015-02-11 15:54:35 -0800586 SensorDevice& device(SensorDevice::getInstance());
Aravind Akella724d91d2013-06-27 12:04:23 -0700587
Aravind Akella4949c502015-02-11 15:54:35 -0800588 for (size_t i = 0; i < batchParams.size(); ++i) {
Peng Xu2bec6232016-07-01 17:13:10 -0700589 if (device.isClientDisabledLocked(batchParams.keyAt(i))) {
590 continue;
Aravind Akella724d91d2013-06-27 12:04:23 -0700591 }
Peng Xu2bec6232016-07-01 17:13:10 -0700592 bestParams.merge(batchParams[i]);
593 }
594 // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly.
595 if (bestParams.mTBatch <= bestParams.mTSample) {
596 bestParams.mTBatch = 0;
Mathias Agopianf001c922010-11-11 17:58:51 -0800597 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700598 bestBatchParams = bestParams;
599}
600
601ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
602 ssize_t idx = batchParams.removeItem(ident);
603 if (idx >= 0) {
604 selectBatchParams();
605 }
606 return idx;
Mathias Agopianf001c922010-11-11 17:58:51 -0800607}
608
Peng Xu4f707f82016-09-26 11:28:32 -0700609void SensorDevice::notifyConnectionDestroyed(void* ident) {
610 Mutex::Autolock _l(mLock);
611 mDisabledClients.remove(ident);
612}
613
Peng Xu53632542017-01-23 20:06:27 -0800614bool SensorDevice::isDirectReportSupported() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800615 return mIsDirectReportSupported;
Peng Xu53632542017-01-23 20:06:27 -0800616}
Steven Morelandd15c0302016-12-20 11:14:50 -0800617
618void SensorDevice::convertToSensorEvent(
619 const Event &src, sensors_event_t *dst) {
620 ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent(
621 src, dst);
622
623 if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) {
624 const DynamicSensorInfo &dyn = src.u.dynamic;
625
626 dst->dynamic_sensor_meta.connected = dyn.connected;
627 dst->dynamic_sensor_meta.handle = dyn.sensorHandle;
628 if (dyn.connected) {
629 auto it = mConnectedDynamicSensors.find(dyn.sensorHandle);
630 CHECK(it != mConnectedDynamicSensors.end());
631
632 dst->dynamic_sensor_meta.sensor = it->second;
633
634 memcpy(dst->dynamic_sensor_meta.uuid,
635 dyn.uuid.data(),
636 sizeof(dst->dynamic_sensor_meta.uuid));
637 }
638 }
639}
640
641void SensorDevice::convertToSensorEvents(
642 const hidl_vec<Event> &src,
643 const hidl_vec<SensorInfo> &dynamicSensorsAdded,
644 sensors_event_t *dst) {
645 // Allocate a sensor_t structure for each dynamic sensor added and insert
646 // it into the dictionary of connected dynamic sensors keyed by handle.
647 for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) {
648 const SensorInfo &info = dynamicSensorsAdded[i];
649
650 auto it = mConnectedDynamicSensors.find(info.sensorHandle);
651 CHECK(it == mConnectedDynamicSensors.end());
652
653 sensor_t *sensor = new sensor_t;
654 convertToSensor(info, sensor);
655
656 mConnectedDynamicSensors.insert(
657 std::make_pair(sensor->handle, sensor));
658 }
659
660 for (size_t i = 0; i < src.size(); ++i) {
661 convertToSensorEvent(src[i], &dst[i]);
662 }
663}
664
Peng Xu3889e6e2017-03-02 19:10:38 -0800665void SensorDevice::handleHidlDeath(const std::string & detail) {
666 // restart is the only option at present.
667 LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str());
668}
669
Mathias Agopianf001c922010-11-11 17:58:51 -0800670// ---------------------------------------------------------------------------
671}; // namespace android