blob: 7bd495fa49807a84db43ef373f4191c9a0bd453e [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
34namespace android {
35// ---------------------------------------------------------------------------
Mathias Agopianf001c922010-11-11 17:58:51 -080036
37ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
38
Steven Morelandd15c0302016-12-20 11:14:50 -080039static status_t StatusFromResult(Result result) {
40 switch (result) {
41 case Result::OK:
42 return OK;
43 case Result::BAD_VALUE:
44 return BAD_VALUE;
45 case Result::PERMISSION_DENIED:
46 return PERMISSION_DENIED;
47 case Result::INVALID_OPERATION:
48 return INVALID_OPERATION;
49 case Result::NO_MEMORY:
50 return NO_MEMORY;
Mathias Agopianf001c922010-11-11 17:58:51 -080051 }
52}
53
Steven Morelandd15c0302016-12-20 11:14:50 -080054SensorDevice::SensorDevice() {
Peng Xu3889e6e2017-03-02 19:10:38 -080055 // SensorDevice may wait upto 100ms * 10 = 1s for hidl service.
56 constexpr auto RETRY_DELAY = std::chrono::milliseconds(100);
57 size_t retry = 10;
Steven Morelandd15c0302016-12-20 11:14:50 -080058
Peng Xu3889e6e2017-03-02 19:10:38 -080059 while (true) {
60 int initStep = 0;
61 mSensors = ISensors::getService();
62 if (mSensors != nullptr) {
63 ++initStep;
64 // Poke ISensor service. If it has lingering connection from previous generation of
65 // system server, it will kill itself. There is no intention to handle the poll result,
66 // which will be done since the size is 0.
67 if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) {
68 // ok to continue
69 break;
70 }
71 // hidl service is restarting, pointer is invalid.
72 mSensors = nullptr;
73 }
74
75 if (--retry <= 0) {
76 ALOGE("Cannot connect to ISensors hidl service!");
77 return;
78 }
79 // Delay 100ms before retry, hidl service is expected to come up in short time after
80 // crash.
81 ALOGI("%s unsuccessful, try again soon (remaining retry %zu).",
82 (initStep == 0) ? "getService()" : "poll() check", retry);
83 std::this_thread::sleep_for(RETRY_DELAY);
Steven Morelandd15c0302016-12-20 11:14:50 -080084 }
85
Peng Xu3889e6e2017-03-02 19:10:38 -080086 checkReturn(mSensors->getSensorsList(
Steven Morelandd15c0302016-12-20 11:14:50 -080087 [&](const auto &list) {
88 const size_t count = list.size();
89
90 mActivationCount.setCapacity(count);
91 Info model;
92 for (size_t i=0 ; i < count; i++) {
93 sensor_t sensor;
94 convertToSensor(list[i], &sensor);
95 mSensorList.push_back(sensor);
96
97 mActivationCount.add(list[i].sensorHandle, model);
98
Peng Xu3889e6e2017-03-02 19:10:38 -080099 checkReturn(mSensors->activate(list[i].sensorHandle, 0 /* enabled */));
Steven Morelandd15c0302016-12-20 11:14:50 -0800100 }
Peng Xu3889e6e2017-03-02 19:10:38 -0800101 }));
Steven Morelandd15c0302016-12-20 11:14:50 -0800102
103 mIsDirectReportSupported =
Peng Xu3889e6e2017-03-02 19:10:38 -0800104 (checkReturn(mSensors->unregisterDirectChannel(-1)) != Result::INVALID_OPERATION);
Steven Morelandd15c0302016-12-20 11:14:50 -0800105}
106
Peng Xu2576cb62016-01-20 00:22:09 -0800107void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) {
108 if (connected) {
109 Info model;
110 mActivationCount.add(handle, model);
Peng Xu3889e6e2017-03-02 19:10:38 -0800111 checkReturn(mSensors->activate(handle, 0 /* enabled */));
Peng Xu2576cb62016-01-20 00:22:09 -0800112 } else {
113 mActivationCount.removeItem(handle);
114 }
115}
116
Peng Xu6a2d3a02015-12-21 12:00:23 -0800117std::string SensorDevice::dump() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800118 if (mSensors == NULL) return "HAL not initialized\n";
Mathias Agopianf001c922010-11-11 17:58:51 -0800119
Peng Xu6a2d3a02015-12-21 12:00:23 -0800120 String8 result;
Peng Xu3889e6e2017-03-02 19:10:38 -0800121 checkReturn(mSensors->getSensorsList([&](const auto &list) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800122 const size_t count = list.size();
Peng Xu6a2d3a02015-12-21 12:00:23 -0800123
Steven Morelandd15c0302016-12-20 11:14:50 -0800124 result.appendFormat(
125 "Total %zu h/w sensors, %zu running:\n",
126 count,
127 mActivationCount.size());
Mathias Agopianf001c922010-11-11 17:58:51 -0800128
Steven Morelandd15c0302016-12-20 11:14:50 -0800129 Mutex::Autolock _l(mLock);
130 for (size_t i = 0 ; i < count ; i++) {
131 const Info& info = mActivationCount.valueFor(
132 list[i].sensorHandle);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800133
Steven Morelandd15c0302016-12-20 11:14:50 -0800134 if (info.batchParams.isEmpty()) continue;
135 result.appendFormat(
136 "0x%08x) active-count = %zu; ",
137 list[i].sensorHandle,
138 info.batchParams.size());
Aravind Akella724d91d2013-06-27 12:04:23 -0700139
Steven Morelandd15c0302016-12-20 11:14:50 -0800140 result.append("sampling_period(ms) = {");
141 for (size_t j = 0; j < info.batchParams.size(); j++) {
142 const BatchParams& params = info.batchParams.valueAt(j);
143 result.appendFormat(
144 "%.1f%s",
145 params.batchDelay / 1e6f,
146 j < info.batchParams.size() - 1 ? ", " : "");
147 }
148 result.appendFormat(
149 "}, selected = %.1f ms; ",
150 info.bestBatchParams.batchDelay / 1e6f);
151
152 result.append("batching_period(ms) = {");
153 for (size_t j = 0; j < info.batchParams.size(); j++) {
154 BatchParams params = info.batchParams.valueAt(j);
155
156 result.appendFormat(
157 "%.1f%s",
158 params.batchTimeout / 1e6f,
159 j < info.batchParams.size() - 1 ? ", " : "");
160 }
161
162 result.appendFormat(
163 "}, selected = %.1f ms\n",
164 info.bestBatchParams.batchTimeout / 1e6f);
165 }
Peng Xu3889e6e2017-03-02 19:10:38 -0800166 }));
Steven Morelandd15c0302016-12-20 11:14:50 -0800167
Peng Xu6a2d3a02015-12-21 12:00:23 -0800168 return result.string();
Mathias Agopianf001c922010-11-11 17:58:51 -0800169}
170
171ssize_t SensorDevice::getSensorList(sensor_t const** list) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800172 *list = &mSensorList[0];
173
174 return mSensorList.size();
Mathias Agopianf001c922010-11-11 17:58:51 -0800175}
176
177status_t SensorDevice::initCheck() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800178 return mSensors != NULL ? NO_ERROR : NO_INIT;
Mathias Agopianf001c922010-11-11 17:58:51 -0800179}
180
181ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800182 if (mSensors == NULL) return NO_INIT;
183
184 ssize_t err;
185
Peng Xu3889e6e2017-03-02 19:10:38 -0800186 checkReturn(mSensors->poll(
Steven Morelandd15c0302016-12-20 11:14:50 -0800187 count,
188 [&](auto result,
189 const auto &events,
190 const auto &dynamicSensorsAdded) {
191 if (result == Result::OK) {
192 convertToSensorEvents(events, dynamicSensorsAdded, buffer);
193 err = (ssize_t)events.size();
194 } else {
195 err = StatusFromResult(result);
196 }
Peng Xu3889e6e2017-03-02 19:10:38 -0800197 }));
Steven Morelandd15c0302016-12-20 11:14:50 -0800198
199 return err;
Mathias Agopianf001c922010-11-11 17:58:51 -0800200}
201
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700202void SensorDevice::autoDisable(void *ident, int handle) {
203 Info& info( mActivationCount.editValueFor(handle) );
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700204 Mutex::Autolock _l(mLock);
Aravind Akella724d91d2013-06-27 12:04:23 -0700205 info.removeBatchParamsForIdent(ident);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700206}
207
Peng Xu6a2d3a02015-12-21 12:00:23 -0800208status_t SensorDevice::activate(void* ident, int handle, int enabled) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800209 if (mSensors == NULL) return NO_INIT;
210
Mathias Agopianf001c922010-11-11 17:58:51 -0800211 status_t err(NO_ERROR);
212 bool actuateHardware = false;
213
Aravind Akella724d91d2013-06-27 12:04:23 -0700214 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800215 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700216
Steve Blocka5512372011-12-20 16:23:08 +0000217 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700218 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
Aravind Akella724d91d2013-06-27 12:04:23 -0700219 ident, handle, enabled, info.batchParams.size());
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700220
Mathias Agopianf001c922010-11-11 17:58:51 -0800221 if (enabled) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700222 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700223
Aravind Akella4949c502015-02-11 15:54:35 -0800224 if (isClientDisabledLocked(ident)) {
Peng Xu966fa882016-09-01 16:13:15 -0700225 ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d",
226 ident, handle);
Aravind Akella4949c502015-02-11 15:54:35 -0800227 return INVALID_OPERATION;
228 }
229
Aravind Akella724d91d2013-06-27 12:04:23 -0700230 if (info.batchParams.indexOfKey(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800231 if (info.numActiveClients() == 1) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700232 // This is the first connection, we need to activate the underlying h/w sensor.
233 actuateHardware = true;
234 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800235 } else {
Aravind Akella724d91d2013-06-27 12:04:23 -0700236 // Log error. Every activate call should be preceded by a batch() call.
237 ALOGE("\t >>>ERROR: activate called without batch");
Mathias Agopianf001c922010-11-11 17:58:51 -0800238 }
239 } else {
Mark Salyzyndb458612014-06-10 14:50:02 -0700240 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700241
Steven Morelandd15c0302016-12-20 11:14:50 -0800242 // If a connected dynamic sensor is deactivated, remove it from the
243 // dictionary.
244 auto it = mConnectedDynamicSensors.find(handle);
245 if (it != mConnectedDynamicSensors.end()) {
246 delete it->second;
247 mConnectedDynamicSensors.erase(it);
248 }
249
Aravind Akella724d91d2013-06-27 12:04:23 -0700250 if (info.removeBatchParamsForIdent(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800251 if (info.numActiveClients() == 0) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700252 // This is the last connection, we need to de-activate the underlying h/w sensor.
Mathias Agopian50b66762010-11-29 17:26:51 -0800253 actuateHardware = true;
Aravind Akella724d91d2013-06-27 12:04:23 -0700254 } else {
Steven Morelandd15c0302016-12-20 11:14:50 -0800255 // Call batch for this sensor with the previously calculated best effort
256 // batch_rate and timeout. One of the apps has unregistered for sensor
257 // events, and the best effort batch parameters might have changed.
258 ALOGD_IF(DEBUG_CONNECTIONS,
259 "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle,
260 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
261 info.bestBatchParams.batchTimeout);
Peng Xu3889e6e2017-03-02 19:10:38 -0800262 checkReturn(mSensors->batch(
Steven Morelandd15c0302016-12-20 11:14:50 -0800263 handle,
264 info.bestBatchParams.batchDelay,
Peng Xu3889e6e2017-03-02 19:10:38 -0800265 info.bestBatchParams.batchTimeout));
Mathias Agopian50b66762010-11-29 17:26:51 -0800266 }
267 } else {
268 // sensor wasn't enabled for this ident
269 }
Aravind Akella4949c502015-02-11 15:54:35 -0800270
271 if (isClientDisabledLocked(ident)) {
272 return NO_ERROR;
273 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800274 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800275
Mathias Agopianf001c922010-11-11 17:58:51 -0800276 if (actuateHardware) {
Aravind Akella4949c502015-02-11 15:54:35 -0800277 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
278 enabled);
Peng Xu3889e6e2017-03-02 19:10:38 -0800279 err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700280 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
281 strerror(-err));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700282
Aravind Akella724d91d2013-06-27 12:04:23 -0700283 if (err != NO_ERROR && enabled) {
284 // Failure when enabling the sensor. Clean up on failure.
285 info.removeBatchParamsForIdent(ident);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700286 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800287 }
288
Mathias Agopianf001c922010-11-11 17:58:51 -0800289 return err;
290}
291
Steven Morelandd15c0302016-12-20 11:14:50 -0800292status_t SensorDevice::batch(
293 void* ident,
294 int handle,
295 int flags,
296 int64_t samplingPeriodNs,
297 int64_t maxBatchReportLatencyNs) {
298 if (mSensors == NULL) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700299
300 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
301 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
302 }
303
Aravind Akella724d91d2013-06-27 12:04:23 -0700304 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700305 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700306 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
307
308 Mutex::Autolock _l(mLock);
309 Info& info(mActivationCount.editValueFor(handle));
310
311 if (info.batchParams.indexOfKey(ident) < 0) {
312 BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs);
313 info.batchParams.add(ident, params);
314 } else {
315 // A batch has already been called with this ident. Update the batch parameters.
316 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
317 }
318
319 BatchParams prevBestBatchParams = info.bestBatchParams;
320 // Find the minimum of all timeouts and batch_rates for this sensor.
321 info.selectBatchParams();
322
323 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700324 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
325 " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700326 prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay,
327 prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout);
328
329 status_t err(NO_ERROR);
330 // If the min period or min timeout has changed since the last batch call, call batch.
331 if (prevBestBatchParams != info.bestBatchParams) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800332 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle,
333 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
334 info.bestBatchParams.batchTimeout);
335 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800336 checkReturn(mSensors->batch(
Steven Morelandd15c0302016-12-20 11:14:50 -0800337 handle,
338 info.bestBatchParams.batchDelay,
Peng Xu3889e6e2017-03-02 19:10:38 -0800339 info.bestBatchParams.batchTimeout)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700340 if (err != NO_ERROR) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700341 ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s",
Steven Morelandd15c0302016-12-20 11:14:50 -0800342 mSensors.get(), handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700343 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
344 info.bestBatchParams.batchTimeout, strerror(-err));
345 info.removeBatchParamsForIdent(ident);
346 }
347 }
348 return err;
349}
350
Peng Xu6a2d3a02015-12-21 12:00:23 -0800351status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800352 if (mSensors == NULL) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700353 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
354 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
355 }
Mathias Agopian667102f2011-09-14 16:43:34 -0700356 Mutex::Autolock _l(mLock);
Aravind Akella4949c502015-02-11 15:54:35 -0800357 if (isClientDisabledLocked(ident)) return INVALID_OPERATION;
Mathias Agopianf001c922010-11-11 17:58:51 -0800358 Info& info( mActivationCount.editValueFor(handle) );
Aravind Akella724d91d2013-06-27 12:04:23 -0700359 // If the underlying sensor is NOT in continuous mode, setDelay() should return an error.
360 // Calling setDelay() in batch mode is an invalid operation.
361 if (info.bestBatchParams.batchTimeout != 0) {
362 return INVALID_OPERATION;
363 }
364 ssize_t index = info.batchParams.indexOfKey(ident);
365 if (index < 0) {
366 return BAD_INDEX;
367 }
368 BatchParams& params = info.batchParams.editValueAt(index);
369 params.batchDelay = samplingPeriodNs;
370 info.selectBatchParams();
Steven Morelandd15c0302016-12-20 11:14:50 -0800371
372 return StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800373 checkReturn(mSensors->batch(handle, info.bestBatchParams.batchDelay, 0)));
Mathias Agopian667102f2011-09-14 16:43:34 -0700374}
375
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700376int SensorDevice::getHalDeviceVersion() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800377 if (mSensors == NULL) return -1;
378 return SENSORS_DEVICE_API_VERSION_1_4;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700379}
380
Aravind Akella9a844cf2014-02-11 18:58:52 -0800381status_t SensorDevice::flush(void* ident, int handle) {
Aravind Akella4949c502015-02-11 15:54:35 -0800382 if (isClientDisabled(ident)) return INVALID_OPERATION;
Aravind Akella724d91d2013-06-27 12:04:23 -0700383 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800384 return StatusFromResult(checkReturn(mSensors->flush(handle)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700385}
386
Aravind Akella4949c502015-02-11 15:54:35 -0800387bool SensorDevice::isClientDisabled(void* ident) {
388 Mutex::Autolock _l(mLock);
389 return isClientDisabledLocked(ident);
390}
391
392bool SensorDevice::isClientDisabledLocked(void* ident) {
393 return mDisabledClients.indexOf(ident) >= 0;
394}
395
396void SensorDevice::enableAllSensors() {
397 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,
410 info.bestBatchParams.batchDelay,
Peng Xu3889e6e2017-03-02 19:10:38 -0800411 info.bestBatchParams.batchTimeout)));
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() {
423 Mutex::Autolock _l(mLock);
Steven Morelandd15c0302016-12-20 11:14:50 -0800424 for (size_t i = 0; i< mActivationCount.size(); ++i) {
Aravind Akella4949c502015-02-11 15:54:35 -0800425 const Info& info = mActivationCount.valueAt(i);
426 // Check if this sensor has been activated previously and disable it.
427 if (info.batchParams.size() > 0) {
428 const int sensor_handle = mActivationCount.keyAt(i);
429 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
430 sensor_handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800431 checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */));
Steven Morelandd15c0302016-12-20 11:14:50 -0800432
Aravind Akella4949c502015-02-11 15:54:35 -0800433 // Add all the connections that were registered for this sensor to the disabled
434 // clients list.
Svetoslavb412f6e2015-04-29 16:50:41 -0700435 for (size_t j = 0; j < info.batchParams.size(); ++j) {
Aravind Akella4949c502015-02-11 15:54:35 -0800436 mDisabledClients.add(info.batchParams.keyAt(j));
Peng Xu966fa882016-09-01 16:13:15 -0700437 ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j));
Aravind Akella4949c502015-02-11 15:54:35 -0800438 }
439 }
440 }
441}
442
Steven Morelandd15c0302016-12-20 11:14:50 -0800443status_t SensorDevice::injectSensorData(
444 const sensors_event_t *injected_sensor_event) {
445 ALOGD_IF(DEBUG_CONNECTIONS,
446 "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
447 injected_sensor_event->sensor,
448 injected_sensor_event->timestamp, injected_sensor_event->data[0],
449 injected_sensor_event->data[1], injected_sensor_event->data[2],
450 injected_sensor_event->data[3], injected_sensor_event->data[4],
451 injected_sensor_event->data[5]);
452
453 Event ev;
454 convertFromSensorEvent(*injected_sensor_event, &ev);
455
Peng Xu3889e6e2017-03-02 19:10:38 -0800456 return StatusFromResult(checkReturn(mSensors->injectSensorData(ev)));
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700457}
458
459status_t SensorDevice::setMode(uint32_t mode) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800460
461 return StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800462 checkReturn(mSensors->setOperationMode(
463 static_cast<hardware::sensors::V1_0::OperationMode>(mode))));
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700464}
465
Mathias Agopian667102f2011-09-14 16:43:34 -0700466// ---------------------------------------------------------------------------
467
Aravind Akella4949c502015-02-11 15:54:35 -0800468int SensorDevice::Info::numActiveClients() {
469 SensorDevice& device(SensorDevice::getInstance());
470 int num = 0;
471 for (size_t i = 0; i < batchParams.size(); ++i) {
472 if (!device.isClientDisabledLocked(batchParams.keyAt(i))) {
473 ++num;
474 }
475 }
476 return num;
477}
478
Aravind Akella724d91d2013-06-27 12:04:23 -0700479status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags,
480 int64_t samplingPeriodNs,
481 int64_t maxBatchReportLatencyNs) {
482 ssize_t index = batchParams.indexOfKey(ident);
Mathias Agopian667102f2011-09-14 16:43:34 -0700483 if (index < 0) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700484 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)",
Aravind Akella724d91d2013-06-27 12:04:23 -0700485 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
Mathias Agopian667102f2011-09-14 16:43:34 -0700486 return BAD_INDEX;
487 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700488 BatchParams& params = batchParams.editValueAt(index);
489 params.flags = flags;
490 params.batchDelay = samplingPeriodNs;
491 params.batchTimeout = maxBatchReportLatencyNs;
Mathias Agopian667102f2011-09-14 16:43:34 -0700492 return NO_ERROR;
493}
494
Aravind Akella724d91d2013-06-27 12:04:23 -0700495void SensorDevice::Info::selectBatchParams() {
Aravind Akella4949c502015-02-11 15:54:35 -0800496 BatchParams bestParams(0, -1, -1);
497 SensorDevice& device(SensorDevice::getInstance());
Aravind Akella724d91d2013-06-27 12:04:23 -0700498
Aravind Akella4949c502015-02-11 15:54:35 -0800499 for (size_t i = 0; i < batchParams.size(); ++i) {
500 if (device.isClientDisabledLocked(batchParams.keyAt(i))) continue;
Aravind Akella724d91d2013-06-27 12:04:23 -0700501 BatchParams params = batchParams.valueAt(i);
Aravind Akella4949c502015-02-11 15:54:35 -0800502 if (bestParams.batchDelay == -1 || params.batchDelay < bestParams.batchDelay) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700503 bestParams.batchDelay = params.batchDelay;
504 }
Aravind Akella4949c502015-02-11 15:54:35 -0800505 if (bestParams.batchTimeout == -1 || params.batchTimeout < bestParams.batchTimeout) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700506 bestParams.batchTimeout = params.batchTimeout;
Mathias Agopianf001c922010-11-11 17:58:51 -0800507 }
508 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700509 bestBatchParams = bestParams;
510}
511
512ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
513 ssize_t idx = batchParams.removeItem(ident);
514 if (idx >= 0) {
515 selectBatchParams();
516 }
517 return idx;
Mathias Agopianf001c922010-11-11 17:58:51 -0800518}
519
Peng Xu4f707f82016-09-26 11:28:32 -0700520void SensorDevice::notifyConnectionDestroyed(void* ident) {
521 Mutex::Autolock _l(mLock);
522 mDisabledClients.remove(ident);
523}
524
Peng Xue36e3472016-11-03 11:57:10 -0700525int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) {
526 Mutex::Autolock _l(mLock);
527
Steven Morelandd15c0302016-12-20 11:14:50 -0800528 SharedMemType type;
529 switch (memory->type) {
530 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
531 type = SharedMemType::ASHMEM;
532 break;
533 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
534 type = SharedMemType::GRALLOC;
535 break;
536 default:
537 return BAD_VALUE;
538 }
539
540 SharedMemFormat format;
541 if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) {
542 return BAD_VALUE;
543 }
544 format = SharedMemFormat::SENSORS_EVENT;
545
546 SharedMemInfo mem = {
547 .type = type,
548 .format = format,
549 .size = static_cast<uint32_t>(memory->size),
550 .memoryHandle = memory->handle,
551 };
552
553 int32_t ret;
Peng Xu3889e6e2017-03-02 19:10:38 -0800554 checkReturn(mSensors->registerDirectChannel(mem,
Steven Morelandd15c0302016-12-20 11:14:50 -0800555 [&ret](auto result, auto channelHandle) {
556 if (result == Result::OK) {
557 ret = channelHandle;
558 } else {
559 ret = StatusFromResult(result);
560 }
Peng Xu3889e6e2017-03-02 19:10:38 -0800561 }));
Steven Morelandd15c0302016-12-20 11:14:50 -0800562 return ret;
Peng Xue36e3472016-11-03 11:57:10 -0700563}
564
565void SensorDevice::unregisterDirectChannel(int32_t channelHandle) {
566 Mutex::Autolock _l(mLock);
Peng Xu3889e6e2017-03-02 19:10:38 -0800567 checkReturn(mSensors->unregisterDirectChannel(channelHandle));
Peng Xue36e3472016-11-03 11:57:10 -0700568}
569
Steven Morelandd15c0302016-12-20 11:14:50 -0800570int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle,
571 int32_t channelHandle, const struct sensors_direct_cfg_t *config) {
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +0000572 Mutex::Autolock _l(mLock);
Steven Morelandd3335112016-12-20 11:14:50 -0800573
Steven Morelandd15c0302016-12-20 11:14:50 -0800574 RateLevel rate;
575 switch(config->rate_level) {
576 case SENSOR_DIRECT_RATE_STOP:
577 rate = RateLevel::STOP;
578 break;
579 case SENSOR_DIRECT_RATE_NORMAL:
580 rate = RateLevel::NORMAL;
581 break;
582 case SENSOR_DIRECT_RATE_FAST:
583 rate = RateLevel::FAST;
584 break;
585 case SENSOR_DIRECT_RATE_VERY_FAST:
586 rate = RateLevel::VERY_FAST;
587 break;
588 default:
589 return BAD_VALUE;
590 }
591
592 int32_t ret;
Peng Xu3889e6e2017-03-02 19:10:38 -0800593 checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate,
Steven Morelandd15c0302016-12-20 11:14:50 -0800594 [&ret, rate] (auto result, auto token) {
595 if (rate == RateLevel::STOP) {
596 ret = StatusFromResult(result);
597 } else {
598 if (result == Result::OK) {
599 ret = token;
600 } else {
601 ret = StatusFromResult(result);
602 }
603 }
Peng Xu3889e6e2017-03-02 19:10:38 -0800604 }));
Steven Morelandd15c0302016-12-20 11:14:50 -0800605
Peng Xue36e3472016-11-03 11:57:10 -0700606 return ret;
607}
Peng Xu53632542017-01-23 20:06:27 -0800608
609bool SensorDevice::isDirectReportSupported() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800610 return mIsDirectReportSupported;
Peng Xu53632542017-01-23 20:06:27 -0800611}
Steven Morelandd15c0302016-12-20 11:14:50 -0800612
613void SensorDevice::convertToSensorEvent(
614 const Event &src, sensors_event_t *dst) {
615 ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent(
616 src, dst);
617
618 if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) {
619 const DynamicSensorInfo &dyn = src.u.dynamic;
620
621 dst->dynamic_sensor_meta.connected = dyn.connected;
622 dst->dynamic_sensor_meta.handle = dyn.sensorHandle;
623 if (dyn.connected) {
624 auto it = mConnectedDynamicSensors.find(dyn.sensorHandle);
625 CHECK(it != mConnectedDynamicSensors.end());
626
627 dst->dynamic_sensor_meta.sensor = it->second;
628
629 memcpy(dst->dynamic_sensor_meta.uuid,
630 dyn.uuid.data(),
631 sizeof(dst->dynamic_sensor_meta.uuid));
632 }
633 }
634}
635
636void SensorDevice::convertToSensorEvents(
637 const hidl_vec<Event> &src,
638 const hidl_vec<SensorInfo> &dynamicSensorsAdded,
639 sensors_event_t *dst) {
640 // Allocate a sensor_t structure for each dynamic sensor added and insert
641 // it into the dictionary of connected dynamic sensors keyed by handle.
642 for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) {
643 const SensorInfo &info = dynamicSensorsAdded[i];
644
645 auto it = mConnectedDynamicSensors.find(info.sensorHandle);
646 CHECK(it == mConnectedDynamicSensors.end());
647
648 sensor_t *sensor = new sensor_t;
649 convertToSensor(info, sensor);
650
651 mConnectedDynamicSensors.insert(
652 std::make_pair(sensor->handle, sensor));
653 }
654
655 for (size_t i = 0; i < src.size(); ++i) {
656 convertToSensorEvent(src[i], &dst[i]);
657 }
658}
659
Peng Xu3889e6e2017-03-02 19:10:38 -0800660void SensorDevice::handleHidlDeath(const std::string & detail) {
661 // restart is the only option at present.
662 LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str());
663}
664
Mathias Agopianf001c922010-11-11 17:58:51 -0800665// ---------------------------------------------------------------------------
666}; // namespace android