blob: 4daae4d141bca76045041ae2fa5c59539255c61e [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>
Steven Moreland2716e112018-02-23 14:57:20 -080021#include <cutils/atomic.h>
Ashutosh Joshi5cafc1e2017-02-09 21:44:04 +000022#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
Steven Morelandd15c0302016-12-20 11:14:50 -080029using namespace android::hardware::sensors::V1_0;
30using namespace android::hardware::sensors::V1_0::implementation;
Peng Xu1a00e2d2017-09-27 23:08:30 -070031using android::hardware::hidl_vec;
32using android::SensorDeviceUtils::HidlServiceRegistrationWaiter;
Peng Xu2bec6232016-07-01 17:13:10 -070033
Mathias Agopianf001c922010-11-11 17:58:51 -080034namespace 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
Peng Xu1a00e2d2017-09-27 23:08:30 -070054SensorDevice::SensorDevice()
55 : mHidlTransportErrors(20), mRestartWaiter(new HidlServiceRegistrationWaiter()) {
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 Xu1a00e2d2017-09-27 23:08:30 -070090 // SensorDevice will wait for HAL service to start if HAL is declared in device manifest.
Peng Xu3889e6e2017-03-02 19:10:38 -080091 size_t retry = 10;
Steven Morelandd15c0302016-12-20 11:14:50 -080092
Peng Xu1a00e2d2017-09-27 23:08:30 -070093 while (retry-- > 0) {
Peng Xu3889e6e2017-03-02 19:10:38 -080094 mSensors = ISensors::getService();
Peng Xu1a00e2d2017-09-27 23:08:30 -070095 if (mSensors == nullptr) {
96 // no sensor hidl service found
Peng Xua8fad9b2017-03-17 12:20:27 -070097 break;
Peng Xu3889e6e2017-03-02 19:10:38 -080098 }
Peng Xu1a00e2d2017-09-27 23:08:30 -070099
100 mRestartWaiter->reset();
101 // Poke ISensor service. If it has lingering connection from previous generation of
102 // system server, it will kill itself. There is no intention to handle the poll result,
103 // which will be done since the size is 0.
104 if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) {
105 // ok to continue
106 break;
107 }
108
109 // hidl service is restarting, pointer is invalid.
110 mSensors = nullptr;
111 ALOGI("%s unsuccessful, remaining retry %zu.", __FUNCTION__, retry);
112 mRestartWaiter->wait();
Steven Morelandd15c0302016-12-20 11:14:50 -0800113 }
Peng Xua8fad9b2017-03-17 12:20:27 -0700114 return (mSensors != nullptr);
Steven Morelandd15c0302016-12-20 11:14:50 -0800115}
116
Peng Xu2576cb62016-01-20 00:22:09 -0800117void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700118 // not need to check mSensors because this is is only called after successful poll()
Peng Xu2576cb62016-01-20 00:22:09 -0800119 if (connected) {
120 Info model;
121 mActivationCount.add(handle, model);
Peng Xu3889e6e2017-03-02 19:10:38 -0800122 checkReturn(mSensors->activate(handle, 0 /* enabled */));
Peng Xu2576cb62016-01-20 00:22:09 -0800123 } else {
124 mActivationCount.removeItem(handle);
125 }
126}
127
Peng Xu6a2d3a02015-12-21 12:00:23 -0800128std::string SensorDevice::dump() const {
Peng Xua8fad9b2017-03-17 12:20:27 -0700129 if (mSensors == nullptr) return "HAL not initialized\n";
Mathias Agopianf001c922010-11-11 17:58:51 -0800130
Peng Xu6a2d3a02015-12-21 12:00:23 -0800131 String8 result;
Peng Xua8fad9b2017-03-17 12:20:27 -0700132 result.appendFormat("Total %zu h/w sensors, %zu running:\n",
133 mSensorList.size(), mActivationCount.size());
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700134
Peng Xua8fad9b2017-03-17 12:20:27 -0700135 Mutex::Autolock _l(mLock);
136 for (const auto & s : mSensorList) {
137 int32_t handle = s.handle;
138 const Info& info = mActivationCount.valueFor(handle);
139 if (info.batchParams.isEmpty()) continue;
140
141 result.appendFormat("0x%08x) active-count = %zu; ", handle, info.batchParams.size());
142
143 result.append("sampling_period(ms) = {");
144 for (size_t j = 0; j < info.batchParams.size(); j++) {
Peng Xu2bec6232016-07-01 17:13:10 -0700145 const BatchParams& params = info.batchParams[j];
146 result.appendFormat("%.1f%s", params.mTSample / 1e6f,
Peng Xua8fad9b2017-03-17 12:20:27 -0700147 j < info.batchParams.size() - 1 ? ", " : "");
148 }
Peng Xu2bec6232016-07-01 17:13:10 -0700149 result.appendFormat("}, selected = %.2f ms; ", info.bestBatchParams.mTSample / 1e6f);
Peng Xua8fad9b2017-03-17 12:20:27 -0700150
151 result.append("batching_period(ms) = {");
152 for (size_t j = 0; j < info.batchParams.size(); j++) {
Peng Xu2bec6232016-07-01 17:13:10 -0700153 const BatchParams& params = info.batchParams[j];
154 result.appendFormat("%.1f%s", params.mTBatch / 1e6f,
Peng Xua8fad9b2017-03-17 12:20:27 -0700155 j < info.batchParams.size() - 1 ? ", " : "");
156 }
Peng Xu2bec6232016-07-01 17:13:10 -0700157 result.appendFormat("}, selected = %.2f ms\n", info.bestBatchParams.mTBatch / 1e6f);
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700158 }
159
Peng Xu6a2d3a02015-12-21 12:00:23 -0800160 return result.string();
Mathias Agopianf001c922010-11-11 17:58:51 -0800161}
162
163ssize_t SensorDevice::getSensorList(sensor_t const** list) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800164 *list = &mSensorList[0];
165
166 return mSensorList.size();
Mathias Agopianf001c922010-11-11 17:58:51 -0800167}
168
169status_t SensorDevice::initCheck() const {
Peng Xu1a00e2d2017-09-27 23:08:30 -0700170 return mSensors != nullptr ? NO_ERROR : NO_INIT;
Mathias Agopianf001c922010-11-11 17:58:51 -0800171}
172
173ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700174 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800175
176 ssize_t err;
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700177 int numHidlTransportErrors = 0;
178 bool hidlTransportError = false;
Steven Morelandd15c0302016-12-20 11:14:50 -0800179
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700180 do {
181 auto ret = mSensors->poll(
182 count,
183 [&](auto result,
184 const auto &events,
185 const auto &dynamicSensorsAdded) {
186 if (result == Result::OK) {
187 convertToSensorEvents(events, dynamicSensorsAdded, buffer);
188 err = (ssize_t)events.size();
189 } else {
190 err = StatusFromResult(result);
191 }
192 });
193
194 if (ret.isOk()) {
195 hidlTransportError = false;
196 } else {
197 hidlTransportError = true;
198 numHidlTransportErrors++;
199 if (numHidlTransportErrors > 50) {
200 // Log error and bail
201 ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors);
202 handleHidlDeath(ret.description());
203 } else {
204 std::this_thread::sleep_for(std::chrono::milliseconds(10));
205 }
206 }
207 } while (hidlTransportError);
208
209 if(numHidlTransportErrors > 0) {
210 ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors);
211 HidlTransportErrorLog errLog(time(NULL), numHidlTransportErrors);
212 mHidlTransportErrors.add(errLog);
213 mTotalHidlTransportErrors++;
214 }
Steven Morelandd15c0302016-12-20 11:14:50 -0800215
216 return err;
Mathias Agopianf001c922010-11-11 17:58:51 -0800217}
218
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700219void SensorDevice::autoDisable(void *ident, int handle) {
220 Info& info( mActivationCount.editValueFor(handle) );
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700221 Mutex::Autolock _l(mLock);
Aravind Akella724d91d2013-06-27 12:04:23 -0700222 info.removeBatchParamsForIdent(ident);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700223}
224
Peng Xu6a2d3a02015-12-21 12:00:23 -0800225status_t SensorDevice::activate(void* ident, int handle, int enabled) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700226 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800227
Mathias Agopianf001c922010-11-11 17:58:51 -0800228 status_t err(NO_ERROR);
229 bool actuateHardware = false;
230
Aravind Akella724d91d2013-06-27 12:04:23 -0700231 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800232 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700233
Steve Blocka5512372011-12-20 16:23:08 +0000234 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700235 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
Aravind Akella724d91d2013-06-27 12:04:23 -0700236 ident, handle, enabled, info.batchParams.size());
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700237
Mathias Agopianf001c922010-11-11 17:58:51 -0800238 if (enabled) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700239 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700240
Aravind Akella4949c502015-02-11 15:54:35 -0800241 if (isClientDisabledLocked(ident)) {
Peng Xu966fa882016-09-01 16:13:15 -0700242 ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d",
243 ident, handle);
Aravind Akella4949c502015-02-11 15:54:35 -0800244 return INVALID_OPERATION;
245 }
246
Aravind Akella724d91d2013-06-27 12:04:23 -0700247 if (info.batchParams.indexOfKey(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800248 if (info.numActiveClients() == 1) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700249 // This is the first connection, we need to activate the underlying h/w sensor.
250 actuateHardware = true;
251 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800252 } else {
Aravind Akella724d91d2013-06-27 12:04:23 -0700253 // Log error. Every activate call should be preceded by a batch() call.
254 ALOGE("\t >>>ERROR: activate called without batch");
Mathias Agopianf001c922010-11-11 17:58:51 -0800255 }
256 } else {
Mark Salyzyndb458612014-06-10 14:50:02 -0700257 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700258
Steven Morelandd15c0302016-12-20 11:14:50 -0800259 // If a connected dynamic sensor is deactivated, remove it from the
260 // dictionary.
261 auto it = mConnectedDynamicSensors.find(handle);
262 if (it != mConnectedDynamicSensors.end()) {
263 delete it->second;
264 mConnectedDynamicSensors.erase(it);
265 }
266
Aravind Akella724d91d2013-06-27 12:04:23 -0700267 if (info.removeBatchParamsForIdent(ident) >= 0) {
Aravind Akella4949c502015-02-11 15:54:35 -0800268 if (info.numActiveClients() == 0) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700269 // This is the last connection, we need to de-activate the underlying h/w sensor.
Mathias Agopian50b66762010-11-29 17:26:51 -0800270 actuateHardware = true;
Aravind Akella724d91d2013-06-27 12:04:23 -0700271 } else {
Steven Morelandd15c0302016-12-20 11:14:50 -0800272 // Call batch for this sensor with the previously calculated best effort
273 // batch_rate and timeout. One of the apps has unregistered for sensor
274 // events, and the best effort batch parameters might have changed.
275 ALOGD_IF(DEBUG_CONNECTIONS,
Peng Xu2bec6232016-07-01 17:13:10 -0700276 "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle,
277 info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch);
Peng Xu3889e6e2017-03-02 19:10:38 -0800278 checkReturn(mSensors->batch(
Peng Xu2bec6232016-07-01 17:13:10 -0700279 handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch));
Mathias Agopian50b66762010-11-29 17:26:51 -0800280 }
281 } else {
282 // sensor wasn't enabled for this ident
283 }
Aravind Akella4949c502015-02-11 15:54:35 -0800284
285 if (isClientDisabledLocked(ident)) {
286 return NO_ERROR;
287 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800288 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800289
Mathias Agopianf001c922010-11-11 17:58:51 -0800290 if (actuateHardware) {
Aravind Akella4949c502015-02-11 15:54:35 -0800291 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle,
292 enabled);
Peng Xu3889e6e2017-03-02 19:10:38 -0800293 err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700294 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
295 strerror(-err));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700296
Aravind Akella724d91d2013-06-27 12:04:23 -0700297 if (err != NO_ERROR && enabled) {
298 // Failure when enabling the sensor. Clean up on failure.
299 info.removeBatchParamsForIdent(ident);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700300 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800301 }
302
Mathias Agopianf001c922010-11-11 17:58:51 -0800303 return err;
304}
305
Steven Morelandd15c0302016-12-20 11:14:50 -0800306status_t SensorDevice::batch(
307 void* ident,
308 int handle,
309 int flags,
310 int64_t samplingPeriodNs,
311 int64_t maxBatchReportLatencyNs) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700312 if (mSensors == nullptr) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700313
314 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
315 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
316 }
Peng Xu2bec6232016-07-01 17:13:10 -0700317 if (maxBatchReportLatencyNs < 0) {
318 maxBatchReportLatencyNs = 0;
319 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700320
Aravind Akella724d91d2013-06-27 12:04:23 -0700321 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700322 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700323 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
324
325 Mutex::Autolock _l(mLock);
326 Info& info(mActivationCount.editValueFor(handle));
327
328 if (info.batchParams.indexOfKey(ident) < 0) {
Peng Xu2bec6232016-07-01 17:13:10 -0700329 BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs);
Aravind Akella724d91d2013-06-27 12:04:23 -0700330 info.batchParams.add(ident, params);
331 } else {
332 // A batch has already been called with this ident. Update the batch parameters.
333 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
334 }
335
336 BatchParams prevBestBatchParams = info.bestBatchParams;
337 // Find the minimum of all timeouts and batch_rates for this sensor.
338 info.selectBatchParams();
339
340 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700341 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
342 " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
Peng Xu2bec6232016-07-01 17:13:10 -0700343 prevBestBatchParams.mTSample, info.bestBatchParams.mTSample,
344 prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch);
Aravind Akella724d91d2013-06-27 12:04:23 -0700345
346 status_t err(NO_ERROR);
347 // If the min period or min timeout has changed since the last batch call, call batch.
348 if (prevBestBatchParams != info.bestBatchParams) {
Peng Xu2bec6232016-07-01 17:13:10 -0700349 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle,
350 info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch);
Steven Morelandd15c0302016-12-20 11:14:50 -0800351 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800352 checkReturn(mSensors->batch(
Peng Xu2bec6232016-07-01 17:13:10 -0700353 handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700354 if (err != NO_ERROR) {
Peng Xu2bec6232016-07-01 17:13:10 -0700355 ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s",
356 mSensors.get(), handle, info.bestBatchParams.mTSample,
357 info.bestBatchParams.mTBatch, strerror(-err));
Aravind Akella724d91d2013-06-27 12:04:23 -0700358 info.removeBatchParamsForIdent(ident);
359 }
360 }
361 return err;
362}
363
Peng Xu6a2d3a02015-12-21 12:00:23 -0800364status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) {
Peng Xu2bec6232016-07-01 17:13:10 -0700365 return batch(ident, handle, 0, samplingPeriodNs, 0);
Mathias Agopian667102f2011-09-14 16:43:34 -0700366}
367
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700368int SensorDevice::getHalDeviceVersion() const {
Peng Xua8fad9b2017-03-17 12:20:27 -0700369 if (mSensors == nullptr) return -1;
Steven Morelandd15c0302016-12-20 11:14:50 -0800370 return SENSORS_DEVICE_API_VERSION_1_4;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700371}
372
Aravind Akella9a844cf2014-02-11 18:58:52 -0800373status_t SensorDevice::flush(void* ident, int handle) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700374 if (mSensors == nullptr) return NO_INIT;
Aravind Akella4949c502015-02-11 15:54:35 -0800375 if (isClientDisabled(ident)) return INVALID_OPERATION;
Aravind Akella724d91d2013-06-27 12:04:23 -0700376 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800377 return StatusFromResult(checkReturn(mSensors->flush(handle)));
Aravind Akella724d91d2013-06-27 12:04:23 -0700378}
379
Aravind Akella4949c502015-02-11 15:54:35 -0800380bool SensorDevice::isClientDisabled(void* ident) {
381 Mutex::Autolock _l(mLock);
382 return isClientDisabledLocked(ident);
383}
384
385bool SensorDevice::isClientDisabledLocked(void* ident) {
386 return mDisabledClients.indexOf(ident) >= 0;
387}
388
389void SensorDevice::enableAllSensors() {
Peng Xua8fad9b2017-03-17 12:20:27 -0700390 if (mSensors == nullptr) return;
Aravind Akella4949c502015-02-11 15:54:35 -0800391 Mutex::Autolock _l(mLock);
392 mDisabledClients.clear();
Peng Xu966fa882016-09-01 16:13:15 -0700393 ALOGI("cleared mDisabledClients");
Aravind Akella4949c502015-02-11 15:54:35 -0800394 for (size_t i = 0; i< mActivationCount.size(); ++i) {
395 Info& info = mActivationCount.editValueAt(i);
396 if (info.batchParams.isEmpty()) continue;
397 info.selectBatchParams();
398 const int sensor_handle = mActivationCount.keyAt(i);
399 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ",
400 sensor_handle);
Steven Morelandd15c0302016-12-20 11:14:50 -0800401 status_t err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800402 checkReturn(mSensors->batch(
Steven Morelandd15c0302016-12-20 11:14:50 -0800403 sensor_handle,
Peng Xu2bec6232016-07-01 17:13:10 -0700404 info.bestBatchParams.mTSample,
405 info.bestBatchParams.mTBatch)));
Steven Morelandd15c0302016-12-20 11:14:50 -0800406 ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err));
Aravind Akella4949c502015-02-11 15:54:35 -0800407
408 if (err == NO_ERROR) {
Steven Morelandd15c0302016-12-20 11:14:50 -0800409 err = StatusFromResult(
Peng Xu3889e6e2017-03-02 19:10:38 -0800410 checkReturn(mSensors->activate(sensor_handle, 1 /* enabled */)));
Aravind Akella4949c502015-02-11 15:54:35 -0800411 ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err));
412 }
Aravind Akella4949c502015-02-11 15:54:35 -0800413 }
414}
415
416void SensorDevice::disableAllSensors() {
Peng Xua8fad9b2017-03-17 12:20:27 -0700417 if (mSensors == nullptr) return;
Aravind Akella4949c502015-02-11 15:54:35 -0800418 Mutex::Autolock _l(mLock);
Peng Xua8fad9b2017-03-17 12:20:27 -0700419 for (size_t i = 0; i< mActivationCount.size(); ++i) {
Aravind Akella4949c502015-02-11 15:54:35 -0800420 const Info& info = mActivationCount.valueAt(i);
421 // Check if this sensor has been activated previously and disable it.
422 if (info.batchParams.size() > 0) {
423 const int sensor_handle = mActivationCount.keyAt(i);
424 ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ",
425 sensor_handle);
Peng Xu3889e6e2017-03-02 19:10:38 -0800426 checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */));
Steven Morelandd15c0302016-12-20 11:14:50 -0800427
Aravind Akella4949c502015-02-11 15:54:35 -0800428 // Add all the connections that were registered for this sensor to the disabled
429 // clients list.
Svetoslavb412f6e2015-04-29 16:50:41 -0700430 for (size_t j = 0; j < info.batchParams.size(); ++j) {
Aravind Akella4949c502015-02-11 15:54:35 -0800431 mDisabledClients.add(info.batchParams.keyAt(j));
Peng Xu966fa882016-09-01 16:13:15 -0700432 ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j));
Aravind Akella4949c502015-02-11 15:54:35 -0800433 }
434 }
435 }
436}
437
Steven Morelandd15c0302016-12-20 11:14:50 -0800438status_t SensorDevice::injectSensorData(
439 const sensors_event_t *injected_sensor_event) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700440 if (mSensors == nullptr) return NO_INIT;
Steven Morelandd15c0302016-12-20 11:14:50 -0800441 ALOGD_IF(DEBUG_CONNECTIONS,
442 "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f",
443 injected_sensor_event->sensor,
444 injected_sensor_event->timestamp, injected_sensor_event->data[0],
445 injected_sensor_event->data[1], injected_sensor_event->data[2],
446 injected_sensor_event->data[3], injected_sensor_event->data[4],
447 injected_sensor_event->data[5]);
448
449 Event ev;
450 convertFromSensorEvent(*injected_sensor_event, &ev);
451
Peng Xu3889e6e2017-03-02 19:10:38 -0800452 return StatusFromResult(checkReturn(mSensors->injectSensorData(ev)));
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700453}
454
455status_t SensorDevice::setMode(uint32_t mode) {
Peng Xua8fad9b2017-03-17 12:20:27 -0700456 if (mSensors == nullptr) return NO_INIT;
457 return StatusFromResult(
458 checkReturn(mSensors->setOperationMode(
459 static_cast<hardware::sensors::V1_0::OperationMode>(mode))));
460}
Steven Morelandd15c0302016-12-20 11:14:50 -0800461
Peng Xua8fad9b2017-03-17 12:20:27 -0700462int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) {
463 if (mSensors == nullptr) return NO_INIT;
464 Mutex::Autolock _l(mLock);
465
466 SharedMemType type;
467 switch (memory->type) {
468 case SENSOR_DIRECT_MEM_TYPE_ASHMEM:
469 type = SharedMemType::ASHMEM;
470 break;
471 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
472 type = SharedMemType::GRALLOC;
473 break;
474 default:
475 return BAD_VALUE;
476 }
477
478 SharedMemFormat format;
479 if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) {
480 return BAD_VALUE;
481 }
482 format = SharedMemFormat::SENSORS_EVENT;
483
484 SharedMemInfo mem = {
485 .type = type,
486 .format = format,
487 .size = static_cast<uint32_t>(memory->size),
488 .memoryHandle = memory->handle,
489 };
490
491 int32_t ret;
492 checkReturn(mSensors->registerDirectChannel(mem,
493 [&ret](auto result, auto channelHandle) {
494 if (result == Result::OK) {
495 ret = channelHandle;
496 } else {
497 ret = StatusFromResult(result);
498 }
499 }));
500 return ret;
501}
502
503void SensorDevice::unregisterDirectChannel(int32_t channelHandle) {
504 if (mSensors == nullptr) return;
505 Mutex::Autolock _l(mLock);
506 checkReturn(mSensors->unregisterDirectChannel(channelHandle));
507}
508
509int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle,
510 int32_t channelHandle, const struct sensors_direct_cfg_t *config) {
511 if (mSensors == nullptr) return NO_INIT;
512 Mutex::Autolock _l(mLock);
513
514 RateLevel rate;
515 switch(config->rate_level) {
516 case SENSOR_DIRECT_RATE_STOP:
517 rate = RateLevel::STOP;
518 break;
519 case SENSOR_DIRECT_RATE_NORMAL:
520 rate = RateLevel::NORMAL;
521 break;
522 case SENSOR_DIRECT_RATE_FAST:
523 rate = RateLevel::FAST;
524 break;
525 case SENSOR_DIRECT_RATE_VERY_FAST:
526 rate = RateLevel::VERY_FAST;
527 break;
528 default:
529 return BAD_VALUE;
530 }
531
532 int32_t ret;
533 checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate,
534 [&ret, rate] (auto result, auto token) {
535 if (rate == RateLevel::STOP) {
536 ret = StatusFromResult(result);
537 } else {
538 if (result == Result::OK) {
539 ret = token;
540 } else {
541 ret = StatusFromResult(result);
542 }
543 }
544 }));
545
546 return ret;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700547}
548
Mathias Agopian667102f2011-09-14 16:43:34 -0700549// ---------------------------------------------------------------------------
550
Aravind Akella4949c502015-02-11 15:54:35 -0800551int SensorDevice::Info::numActiveClients() {
552 SensorDevice& device(SensorDevice::getInstance());
553 int num = 0;
554 for (size_t i = 0; i < batchParams.size(); ++i) {
555 if (!device.isClientDisabledLocked(batchParams.keyAt(i))) {
556 ++num;
557 }
558 }
559 return num;
560}
561
Peng Xu2bec6232016-07-01 17:13:10 -0700562status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int,
Aravind Akella724d91d2013-06-27 12:04:23 -0700563 int64_t samplingPeriodNs,
564 int64_t maxBatchReportLatencyNs) {
565 ssize_t index = batchParams.indexOfKey(ident);
Mathias Agopian667102f2011-09-14 16:43:34 -0700566 if (index < 0) {
Peng Xu2bec6232016-07-01 17:13:10 -0700567 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64
568 " timeout=%" PRId64 ") failed (%s)",
Aravind Akella724d91d2013-06-27 12:04:23 -0700569 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
Mathias Agopian667102f2011-09-14 16:43:34 -0700570 return BAD_INDEX;
571 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700572 BatchParams& params = batchParams.editValueAt(index);
Peng Xu2bec6232016-07-01 17:13:10 -0700573 params.mTSample = samplingPeriodNs;
574 params.mTBatch = maxBatchReportLatencyNs;
Mathias Agopian667102f2011-09-14 16:43:34 -0700575 return NO_ERROR;
576}
577
Aravind Akella724d91d2013-06-27 12:04:23 -0700578void SensorDevice::Info::selectBatchParams() {
Peng Xu2bec6232016-07-01 17:13:10 -0700579 BatchParams bestParams; // default to max Tsample and max Tbatch
Aravind Akella4949c502015-02-11 15:54:35 -0800580 SensorDevice& device(SensorDevice::getInstance());
Aravind Akella724d91d2013-06-27 12:04:23 -0700581
Aravind Akella4949c502015-02-11 15:54:35 -0800582 for (size_t i = 0; i < batchParams.size(); ++i) {
Peng Xu2bec6232016-07-01 17:13:10 -0700583 if (device.isClientDisabledLocked(batchParams.keyAt(i))) {
584 continue;
Aravind Akella724d91d2013-06-27 12:04:23 -0700585 }
Peng Xu2bec6232016-07-01 17:13:10 -0700586 bestParams.merge(batchParams[i]);
587 }
588 // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly.
589 if (bestParams.mTBatch <= bestParams.mTSample) {
590 bestParams.mTBatch = 0;
Mathias Agopianf001c922010-11-11 17:58:51 -0800591 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700592 bestBatchParams = bestParams;
593}
594
595ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
596 ssize_t idx = batchParams.removeItem(ident);
597 if (idx >= 0) {
598 selectBatchParams();
599 }
600 return idx;
Mathias Agopianf001c922010-11-11 17:58:51 -0800601}
602
Peng Xu4f707f82016-09-26 11:28:32 -0700603void SensorDevice::notifyConnectionDestroyed(void* ident) {
604 Mutex::Autolock _l(mLock);
605 mDisabledClients.remove(ident);
606}
607
Peng Xu53632542017-01-23 20:06:27 -0800608bool SensorDevice::isDirectReportSupported() const {
Steven Morelandd15c0302016-12-20 11:14:50 -0800609 return mIsDirectReportSupported;
Peng Xu53632542017-01-23 20:06:27 -0800610}
Steven Morelandd15c0302016-12-20 11:14:50 -0800611
612void SensorDevice::convertToSensorEvent(
613 const Event &src, sensors_event_t *dst) {
614 ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent(
615 src, dst);
616
617 if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) {
618 const DynamicSensorInfo &dyn = src.u.dynamic;
619
620 dst->dynamic_sensor_meta.connected = dyn.connected;
621 dst->dynamic_sensor_meta.handle = dyn.sensorHandle;
622 if (dyn.connected) {
623 auto it = mConnectedDynamicSensors.find(dyn.sensorHandle);
624 CHECK(it != mConnectedDynamicSensors.end());
625
626 dst->dynamic_sensor_meta.sensor = it->second;
627
628 memcpy(dst->dynamic_sensor_meta.uuid,
629 dyn.uuid.data(),
630 sizeof(dst->dynamic_sensor_meta.uuid));
631 }
632 }
633}
634
635void SensorDevice::convertToSensorEvents(
636 const hidl_vec<Event> &src,
637 const hidl_vec<SensorInfo> &dynamicSensorsAdded,
638 sensors_event_t *dst) {
639 // Allocate a sensor_t structure for each dynamic sensor added and insert
640 // it into the dictionary of connected dynamic sensors keyed by handle.
641 for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) {
642 const SensorInfo &info = dynamicSensorsAdded[i];
643
644 auto it = mConnectedDynamicSensors.find(info.sensorHandle);
645 CHECK(it == mConnectedDynamicSensors.end());
646
647 sensor_t *sensor = new sensor_t;
648 convertToSensor(info, sensor);
649
650 mConnectedDynamicSensors.insert(
651 std::make_pair(sensor->handle, sensor));
652 }
653
654 for (size_t i = 0; i < src.size(); ++i) {
655 convertToSensorEvent(src[i], &dst[i]);
656 }
657}
658
Peng Xu3889e6e2017-03-02 19:10:38 -0800659void SensorDevice::handleHidlDeath(const std::string & detail) {
660 // restart is the only option at present.
661 LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str());
662}
663
Mathias Agopianf001c922010-11-11 17:58:51 -0800664// ---------------------------------------------------------------------------
665}; // namespace android