Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 16 | #include "SensorDevice.h" |
| 17 | #include "SensorService.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 18 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 20 | #include <sensor/SensorEventQueue.h> |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 21 | #include <sensors/convert.h> |
Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 22 | #include <cutils/atomic.h> |
Ashutosh Joshi | 5cafc1e | 2017-02-09 21:44:04 +0000 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | #include <utils/Singleton.h> |
Steven Moreland | d333511 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 25 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 26 | #include <chrono> |
| 27 | #include <cinttypes> |
| 28 | #include <thread> |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 29 | |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 30 | using namespace android::hardware::sensors; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 31 | using namespace android::hardware::sensors::V1_0; |
| 32 | using namespace android::hardware::sensors::V1_0::implementation; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 33 | using android::hardware::hidl_vec; |
| 34 | using android::SensorDeviceUtils::HidlServiceRegistrationWaiter; |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | // --------------------------------------------------------------------------- |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 38 | |
| 39 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) |
| 40 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 41 | static status_t StatusFromResult(Result result) { |
| 42 | switch (result) { |
| 43 | case Result::OK: |
| 44 | return OK; |
| 45 | case Result::BAD_VALUE: |
| 46 | return BAD_VALUE; |
| 47 | case Result::PERMISSION_DENIED: |
| 48 | return PERMISSION_DENIED; |
| 49 | case Result::INVALID_OPERATION: |
| 50 | return INVALID_OPERATION; |
| 51 | case Result::NO_MEMORY: |
| 52 | return NO_MEMORY; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 56 | SensorDevice::SensorDevice() |
| 57 | : mHidlTransportErrors(20), mRestartWaiter(new HidlServiceRegistrationWaiter()) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 58 | if (!connectHidlService()) { |
| 59 | return; |
| 60 | } |
Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 61 | |
| 62 | float minPowerMa = 0.001; // 1 microAmp |
| 63 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 64 | checkReturn(mSensors->getSensorsList( |
| 65 | [&](const auto &list) { |
| 66 | const size_t count = list.size(); |
| 67 | |
| 68 | mActivationCount.setCapacity(count); |
| 69 | Info model; |
| 70 | for (size_t i=0 ; i < count; i++) { |
| 71 | sensor_t sensor; |
| 72 | convertToSensor(list[i], &sensor); |
Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 73 | // Sanity check and clamp power if it is 0 (or close) |
| 74 | if (sensor.power < minPowerMa) { |
| 75 | ALOGE("Reported power %f not deemed sane, clamping to %f", |
| 76 | sensor.power, minPowerMa); |
| 77 | sensor.power = minPowerMa; |
| 78 | } |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 79 | mSensorList.push_back(sensor); |
| 80 | |
| 81 | mActivationCount.add(list[i].sensorHandle, model); |
| 82 | |
| 83 | checkReturn(mSensors->activate(list[i].sensorHandle, 0 /* enabled */)); |
| 84 | } |
| 85 | })); |
| 86 | |
| 87 | mIsDirectReportSupported = |
| 88 | (checkReturn(mSensors->unregisterDirectChannel(-1)) != Result::INVALID_OPERATION); |
| 89 | } |
| 90 | |
| 91 | bool SensorDevice::connectHidlService() { |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 92 | HalConnectionStatus status = connectHidlServiceV2_0(); |
| 93 | if (status == HalConnectionStatus::DOES_NOT_EXIST) { |
| 94 | status = connectHidlServiceV1_0(); |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 95 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 96 | return (status == HalConnectionStatus::CONNECTED); |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 99 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV1_0() { |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 100 | // SensorDevice will wait for HAL service to start if HAL is declared in device manifest. |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 101 | size_t retry = 10; |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 102 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 103 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 104 | while (retry-- > 0) { |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 105 | sp<V1_0::ISensors> sensors = V1_0::ISensors::getService(); |
| 106 | if (sensors == nullptr) { |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 107 | // no sensor hidl service found |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 108 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 109 | break; |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 110 | } |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 111 | |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 112 | mSensors = new SensorServiceUtil::SensorsWrapperV1_0(sensors); |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 113 | mRestartWaiter->reset(); |
| 114 | // Poke ISensor service. If it has lingering connection from previous generation of |
| 115 | // system server, it will kill itself. There is no intention to handle the poll result, |
| 116 | // which will be done since the size is 0. |
| 117 | if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) { |
| 118 | // ok to continue |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 119 | connectionStatus = HalConnectionStatus::CONNECTED; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | |
| 123 | // hidl service is restarting, pointer is invalid. |
| 124 | mSensors = nullptr; |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 125 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 126 | ALOGI("%s unsuccessful, remaining retry %zu.", __FUNCTION__, retry); |
| 127 | mRestartWaiter->wait(); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 128 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 129 | |
| 130 | return connectionStatus; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 133 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_0() { |
| 134 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 135 | sp<V2_0::ISensors> sensors = V2_0::ISensors::getService(); |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 136 | |
| 137 | if (sensors == nullptr) { |
| 138 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; |
| 139 | } else { |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 140 | mSensors = new SensorServiceUtil::SensorsWrapperV2_0(sensors); |
| 141 | |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 142 | mEventQueue = std::make_unique<EventMessageQueue>( |
| 143 | SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT, |
| 144 | true /* configureEventFlagWord */); |
| 145 | |
| 146 | mWakeLockQueue = std::make_unique<WakeLockQueue>( |
| 147 | SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT, |
| 148 | true /* configureEventFlagWord */); |
| 149 | |
| 150 | CHECK(mSensors != nullptr && mEventQueue != nullptr && |
| 151 | mWakeLockQueue != nullptr); |
| 152 | |
| 153 | status_t status = StatusFromResult(checkReturn(mSensors->initializeMessageQueues( |
| 154 | *mEventQueue->getDesc(), |
| 155 | *mWakeLockQueue->getDesc()))); |
| 156 | |
| 157 | if (status != NO_ERROR) { |
| 158 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; |
| 159 | ALOGE("Failed to initialize message queues (%s)", strerror(-status)); |
| 160 | } else { |
| 161 | connectionStatus = HalConnectionStatus::CONNECTED; |
| 162 | } |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 163 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame^] | 164 | |
| 165 | return connectionStatus; |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 168 | void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 169 | // not need to check mSensors because this is is only called after successful poll() |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 170 | if (connected) { |
| 171 | Info model; |
| 172 | mActivationCount.add(handle, model); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 173 | checkReturn(mSensors->activate(handle, 0 /* enabled */)); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 174 | } else { |
| 175 | mActivationCount.removeItem(handle); |
| 176 | } |
| 177 | } |
| 178 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 179 | std::string SensorDevice::dump() const { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 180 | if (mSensors == nullptr) return "HAL not initialized\n"; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 181 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 182 | String8 result; |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 183 | result.appendFormat("Total %zu h/w sensors, %zu running:\n", |
| 184 | mSensorList.size(), mActivationCount.size()); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 185 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 186 | Mutex::Autolock _l(mLock); |
| 187 | for (const auto & s : mSensorList) { |
| 188 | int32_t handle = s.handle; |
| 189 | const Info& info = mActivationCount.valueFor(handle); |
| 190 | if (info.batchParams.isEmpty()) continue; |
| 191 | |
| 192 | result.appendFormat("0x%08x) active-count = %zu; ", handle, info.batchParams.size()); |
| 193 | |
| 194 | result.append("sampling_period(ms) = {"); |
| 195 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 196 | const BatchParams& params = info.batchParams[j]; |
| 197 | result.appendFormat("%.1f%s", params.mTSample / 1e6f, |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 198 | j < info.batchParams.size() - 1 ? ", " : ""); |
| 199 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 200 | result.appendFormat("}, selected = %.2f ms; ", info.bestBatchParams.mTSample / 1e6f); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 201 | |
| 202 | result.append("batching_period(ms) = {"); |
| 203 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 204 | const BatchParams& params = info.batchParams[j]; |
| 205 | result.appendFormat("%.1f%s", params.mTBatch / 1e6f, |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 206 | j < info.batchParams.size() - 1 ? ", " : ""); |
| 207 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 208 | result.appendFormat("}, selected = %.2f ms\n", info.bestBatchParams.mTBatch / 1e6f); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 211 | return result.string(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 215 | *list = &mSensorList[0]; |
| 216 | |
| 217 | return mSensorList.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | status_t SensorDevice::initCheck() const { |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 221 | return mSensors != nullptr ? NO_ERROR : NO_INIT; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 225 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 226 | |
| 227 | ssize_t err; |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 228 | int numHidlTransportErrors = 0; |
| 229 | bool hidlTransportError = false; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 230 | |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 231 | do { |
| 232 | auto ret = mSensors->poll( |
| 233 | count, |
| 234 | [&](auto result, |
| 235 | const auto &events, |
| 236 | const auto &dynamicSensorsAdded) { |
| 237 | if (result == Result::OK) { |
| 238 | convertToSensorEvents(events, dynamicSensorsAdded, buffer); |
| 239 | err = (ssize_t)events.size(); |
| 240 | } else { |
| 241 | err = StatusFromResult(result); |
| 242 | } |
| 243 | }); |
| 244 | |
| 245 | if (ret.isOk()) { |
| 246 | hidlTransportError = false; |
| 247 | } else { |
| 248 | hidlTransportError = true; |
| 249 | numHidlTransportErrors++; |
| 250 | if (numHidlTransportErrors > 50) { |
| 251 | // Log error and bail |
| 252 | ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors); |
| 253 | handleHidlDeath(ret.description()); |
| 254 | } else { |
| 255 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 256 | } |
| 257 | } |
| 258 | } while (hidlTransportError); |
| 259 | |
| 260 | if(numHidlTransportErrors > 0) { |
| 261 | ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors); |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 262 | HidlTransportErrorLog errLog(time(nullptr), numHidlTransportErrors); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 263 | mHidlTransportErrors.add(errLog); |
| 264 | mTotalHidlTransportErrors++; |
| 265 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 266 | |
| 267 | return err; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 270 | void SensorDevice::autoDisable(void *ident, int handle) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 271 | Mutex::Autolock _l(mLock); |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 272 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 273 | if (activationIndex < 0) { |
| 274 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 275 | return; |
| 276 | } |
| 277 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 278 | info.removeBatchParamsForIdent(ident); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 281 | status_t SensorDevice::activate(void* ident, int handle, int enabled) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 282 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 283 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 284 | status_t err(NO_ERROR); |
| 285 | bool actuateHardware = false; |
| 286 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 287 | Mutex::Autolock _l(mLock); |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 288 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 289 | if (activationIndex < 0) { |
| 290 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 291 | return BAD_VALUE; |
| 292 | } |
| 293 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 294 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 295 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 296 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 297 | ident, handle, enabled, info.batchParams.size()); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 298 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 299 | if (enabled) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 300 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 301 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 302 | if (isClientDisabledLocked(ident)) { |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 303 | ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d", |
| 304 | ident, handle); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 305 | return INVALID_OPERATION; |
| 306 | } |
| 307 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 308 | if (info.batchParams.indexOfKey(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 309 | if (info.numActiveClients() == 1) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 310 | // This is the first connection, we need to activate the underlying h/w sensor. |
| 311 | actuateHardware = true; |
| 312 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 313 | } else { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 314 | // Log error. Every activate call should be preceded by a batch() call. |
| 315 | ALOGE("\t >>>ERROR: activate called without batch"); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 316 | } |
| 317 | } else { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 318 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 319 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 320 | // If a connected dynamic sensor is deactivated, remove it from the |
| 321 | // dictionary. |
| 322 | auto it = mConnectedDynamicSensors.find(handle); |
| 323 | if (it != mConnectedDynamicSensors.end()) { |
| 324 | delete it->second; |
| 325 | mConnectedDynamicSensors.erase(it); |
| 326 | } |
| 327 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 328 | if (info.removeBatchParamsForIdent(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 329 | if (info.numActiveClients() == 0) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 330 | // This is the last connection, we need to de-activate the underlying h/w sensor. |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 331 | actuateHardware = true; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 332 | } else { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 333 | // Call batch for this sensor with the previously calculated best effort |
| 334 | // batch_rate and timeout. One of the apps has unregistered for sensor |
| 335 | // events, and the best effort batch parameters might have changed. |
| 336 | ALOGD_IF(DEBUG_CONNECTIONS, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 337 | "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle, |
| 338 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 339 | checkReturn(mSensors->batch( |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 340 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)); |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 341 | } |
| 342 | } else { |
| 343 | // sensor wasn't enabled for this ident |
| 344 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 345 | |
| 346 | if (isClientDisabledLocked(ident)) { |
| 347 | return NO_ERROR; |
| 348 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 349 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 350 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 351 | if (actuateHardware) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 352 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, |
| 353 | enabled); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 354 | err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled))); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 355 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, |
| 356 | strerror(-err)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 357 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 358 | if (err != NO_ERROR && enabled) { |
| 359 | // Failure when enabling the sensor. Clean up on failure. |
| 360 | info.removeBatchParamsForIdent(ident); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 361 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 364 | return err; |
| 365 | } |
| 366 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 367 | status_t SensorDevice::batch( |
| 368 | void* ident, |
| 369 | int handle, |
| 370 | int flags, |
| 371 | int64_t samplingPeriodNs, |
| 372 | int64_t maxBatchReportLatencyNs) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 373 | if (mSensors == nullptr) return NO_INIT; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 374 | |
| 375 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 376 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 377 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 378 | if (maxBatchReportLatencyNs < 0) { |
| 379 | maxBatchReportLatencyNs = 0; |
| 380 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 381 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 382 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 383 | "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 384 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 385 | |
| 386 | Mutex::Autolock _l(mLock); |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 387 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 388 | if (activationIndex < 0) { |
| 389 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 390 | return BAD_VALUE; |
| 391 | } |
| 392 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 393 | |
| 394 | if (info.batchParams.indexOfKey(ident) < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 395 | BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 396 | info.batchParams.add(ident, params); |
| 397 | } else { |
| 398 | // A batch has already been called with this ident. Update the batch parameters. |
| 399 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 400 | } |
| 401 | |
| 402 | BatchParams prevBestBatchParams = info.bestBatchParams; |
| 403 | // Find the minimum of all timeouts and batch_rates for this sensor. |
| 404 | info.selectBatchParams(); |
| 405 | |
| 406 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 407 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 |
| 408 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 409 | prevBestBatchParams.mTSample, info.bestBatchParams.mTSample, |
| 410 | prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 411 | |
| 412 | status_t err(NO_ERROR); |
| 413 | // If the min period or min timeout has changed since the last batch call, call batch. |
| 414 | if (prevBestBatchParams != info.bestBatchParams) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 415 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle, |
| 416 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 417 | err = StatusFromResult( |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 418 | checkReturn(mSensors->batch( |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 419 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch))); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 420 | if (err != NO_ERROR) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 421 | ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s", |
| 422 | mSensors.get(), handle, info.bestBatchParams.mTSample, |
| 423 | info.bestBatchParams.mTBatch, strerror(-err)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 424 | info.removeBatchParamsForIdent(ident); |
| 425 | } |
| 426 | } |
| 427 | return err; |
| 428 | } |
| 429 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 430 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 431 | return batch(ident, handle, 0, samplingPeriodNs, 0); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 434 | int SensorDevice::getHalDeviceVersion() const { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 435 | if (mSensors == nullptr) return -1; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 436 | return SENSORS_DEVICE_API_VERSION_1_4; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 439 | status_t SensorDevice::flush(void* ident, int handle) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 440 | if (mSensors == nullptr) return NO_INIT; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 441 | if (isClientDisabled(ident)) return INVALID_OPERATION; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 442 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 443 | return StatusFromResult(checkReturn(mSensors->flush(handle))); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 446 | bool SensorDevice::isClientDisabled(void* ident) { |
| 447 | Mutex::Autolock _l(mLock); |
| 448 | return isClientDisabledLocked(ident); |
| 449 | } |
| 450 | |
| 451 | bool SensorDevice::isClientDisabledLocked(void* ident) { |
| 452 | return mDisabledClients.indexOf(ident) >= 0; |
| 453 | } |
| 454 | |
| 455 | void SensorDevice::enableAllSensors() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 456 | if (mSensors == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 457 | Mutex::Autolock _l(mLock); |
| 458 | mDisabledClients.clear(); |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 459 | ALOGI("cleared mDisabledClients"); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 460 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
| 461 | Info& info = mActivationCount.editValueAt(i); |
| 462 | if (info.batchParams.isEmpty()) continue; |
| 463 | info.selectBatchParams(); |
| 464 | const int sensor_handle = mActivationCount.keyAt(i); |
| 465 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", |
| 466 | sensor_handle); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 467 | status_t err = StatusFromResult( |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 468 | checkReturn(mSensors->batch( |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 469 | sensor_handle, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 470 | info.bestBatchParams.mTSample, |
| 471 | info.bestBatchParams.mTBatch))); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 472 | ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err)); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 473 | |
| 474 | if (err == NO_ERROR) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 475 | err = StatusFromResult( |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 476 | checkReturn(mSensors->activate(sensor_handle, 1 /* enabled */))); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 477 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); |
| 478 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | void SensorDevice::disableAllSensors() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 483 | if (mSensors == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 484 | Mutex::Autolock _l(mLock); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 485 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 486 | const Info& info = mActivationCount.valueAt(i); |
| 487 | // Check if this sensor has been activated previously and disable it. |
| 488 | if (info.batchParams.size() > 0) { |
| 489 | const int sensor_handle = mActivationCount.keyAt(i); |
| 490 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", |
| 491 | sensor_handle); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 492 | checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 493 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 494 | // Add all the connections that were registered for this sensor to the disabled |
| 495 | // clients list. |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 496 | for (size_t j = 0; j < info.batchParams.size(); ++j) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 497 | mDisabledClients.add(info.batchParams.keyAt(j)); |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 498 | ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j)); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 504 | status_t SensorDevice::injectSensorData( |
| 505 | const sensors_event_t *injected_sensor_event) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 506 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 507 | ALOGD_IF(DEBUG_CONNECTIONS, |
| 508 | "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", |
| 509 | injected_sensor_event->sensor, |
| 510 | injected_sensor_event->timestamp, injected_sensor_event->data[0], |
| 511 | injected_sensor_event->data[1], injected_sensor_event->data[2], |
| 512 | injected_sensor_event->data[3], injected_sensor_event->data[4], |
| 513 | injected_sensor_event->data[5]); |
| 514 | |
| 515 | Event ev; |
| 516 | convertFromSensorEvent(*injected_sensor_event, &ev); |
| 517 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 518 | return StatusFromResult(checkReturn(mSensors->injectSensorData(ev))); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | status_t SensorDevice::setMode(uint32_t mode) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 522 | if (mSensors == nullptr) return NO_INIT; |
| 523 | return StatusFromResult( |
| 524 | checkReturn(mSensors->setOperationMode( |
| 525 | static_cast<hardware::sensors::V1_0::OperationMode>(mode)))); |
| 526 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 527 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 528 | int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) { |
| 529 | if (mSensors == nullptr) return NO_INIT; |
| 530 | Mutex::Autolock _l(mLock); |
| 531 | |
| 532 | SharedMemType type; |
| 533 | switch (memory->type) { |
| 534 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: |
| 535 | type = SharedMemType::ASHMEM; |
| 536 | break; |
| 537 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: |
| 538 | type = SharedMemType::GRALLOC; |
| 539 | break; |
| 540 | default: |
| 541 | return BAD_VALUE; |
| 542 | } |
| 543 | |
| 544 | SharedMemFormat format; |
| 545 | if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { |
| 546 | return BAD_VALUE; |
| 547 | } |
| 548 | format = SharedMemFormat::SENSORS_EVENT; |
| 549 | |
| 550 | SharedMemInfo mem = { |
| 551 | .type = type, |
| 552 | .format = format, |
| 553 | .size = static_cast<uint32_t>(memory->size), |
| 554 | .memoryHandle = memory->handle, |
| 555 | }; |
| 556 | |
| 557 | int32_t ret; |
| 558 | checkReturn(mSensors->registerDirectChannel(mem, |
| 559 | [&ret](auto result, auto channelHandle) { |
| 560 | if (result == Result::OK) { |
| 561 | ret = channelHandle; |
| 562 | } else { |
| 563 | ret = StatusFromResult(result); |
| 564 | } |
| 565 | })); |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | void SensorDevice::unregisterDirectChannel(int32_t channelHandle) { |
| 570 | if (mSensors == nullptr) return; |
| 571 | Mutex::Autolock _l(mLock); |
| 572 | checkReturn(mSensors->unregisterDirectChannel(channelHandle)); |
| 573 | } |
| 574 | |
| 575 | int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, |
| 576 | int32_t channelHandle, const struct sensors_direct_cfg_t *config) { |
| 577 | if (mSensors == nullptr) return NO_INIT; |
| 578 | Mutex::Autolock _l(mLock); |
| 579 | |
| 580 | RateLevel rate; |
| 581 | switch(config->rate_level) { |
| 582 | case SENSOR_DIRECT_RATE_STOP: |
| 583 | rate = RateLevel::STOP; |
| 584 | break; |
| 585 | case SENSOR_DIRECT_RATE_NORMAL: |
| 586 | rate = RateLevel::NORMAL; |
| 587 | break; |
| 588 | case SENSOR_DIRECT_RATE_FAST: |
| 589 | rate = RateLevel::FAST; |
| 590 | break; |
| 591 | case SENSOR_DIRECT_RATE_VERY_FAST: |
| 592 | rate = RateLevel::VERY_FAST; |
| 593 | break; |
| 594 | default: |
| 595 | return BAD_VALUE; |
| 596 | } |
| 597 | |
| 598 | int32_t ret; |
| 599 | checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate, |
| 600 | [&ret, rate] (auto result, auto token) { |
| 601 | if (rate == RateLevel::STOP) { |
| 602 | ret = StatusFromResult(result); |
| 603 | } else { |
| 604 | if (result == Result::OK) { |
| 605 | ret = token; |
| 606 | } else { |
| 607 | ret = StatusFromResult(result); |
| 608 | } |
| 609 | } |
| 610 | })); |
| 611 | |
| 612 | return ret; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 615 | // --------------------------------------------------------------------------- |
| 616 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 617 | int SensorDevice::Info::numActiveClients() { |
| 618 | SensorDevice& device(SensorDevice::getInstance()); |
| 619 | int num = 0; |
| 620 | for (size_t i = 0; i < batchParams.size(); ++i) { |
| 621 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 622 | ++num; |
| 623 | } |
| 624 | } |
| 625 | return num; |
| 626 | } |
| 627 | |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 628 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 629 | int64_t samplingPeriodNs, |
| 630 | int64_t maxBatchReportLatencyNs) { |
| 631 | ssize_t index = batchParams.indexOfKey(ident); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 632 | if (index < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 633 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 |
| 634 | " timeout=%" PRId64 ") failed (%s)", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 635 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 636 | return BAD_INDEX; |
| 637 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 638 | BatchParams& params = batchParams.editValueAt(index); |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 639 | params.mTSample = samplingPeriodNs; |
| 640 | params.mTBatch = maxBatchReportLatencyNs; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 641 | return NO_ERROR; |
| 642 | } |
| 643 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 644 | void SensorDevice::Info::selectBatchParams() { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 645 | BatchParams bestParams; // default to max Tsample and max Tbatch |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 646 | SensorDevice& device(SensorDevice::getInstance()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 647 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 648 | for (size_t i = 0; i < batchParams.size(); ++i) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 649 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 650 | continue; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 651 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 652 | bestParams.merge(batchParams[i]); |
| 653 | } |
| 654 | // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly. |
| 655 | if (bestParams.mTBatch <= bestParams.mTSample) { |
| 656 | bestParams.mTBatch = 0; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 657 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 658 | bestBatchParams = bestParams; |
| 659 | } |
| 660 | |
| 661 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { |
| 662 | ssize_t idx = batchParams.removeItem(ident); |
| 663 | if (idx >= 0) { |
| 664 | selectBatchParams(); |
| 665 | } |
| 666 | return idx; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 669 | void SensorDevice::notifyConnectionDestroyed(void* ident) { |
| 670 | Mutex::Autolock _l(mLock); |
| 671 | mDisabledClients.remove(ident); |
| 672 | } |
| 673 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 674 | bool SensorDevice::isDirectReportSupported() const { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 675 | return mIsDirectReportSupported; |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 676 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 677 | |
| 678 | void SensorDevice::convertToSensorEvent( |
| 679 | const Event &src, sensors_event_t *dst) { |
| 680 | ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent( |
| 681 | src, dst); |
| 682 | |
| 683 | if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) { |
| 684 | const DynamicSensorInfo &dyn = src.u.dynamic; |
| 685 | |
| 686 | dst->dynamic_sensor_meta.connected = dyn.connected; |
| 687 | dst->dynamic_sensor_meta.handle = dyn.sensorHandle; |
| 688 | if (dyn.connected) { |
| 689 | auto it = mConnectedDynamicSensors.find(dyn.sensorHandle); |
| 690 | CHECK(it != mConnectedDynamicSensors.end()); |
| 691 | |
| 692 | dst->dynamic_sensor_meta.sensor = it->second; |
| 693 | |
| 694 | memcpy(dst->dynamic_sensor_meta.uuid, |
| 695 | dyn.uuid.data(), |
| 696 | sizeof(dst->dynamic_sensor_meta.uuid)); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | void SensorDevice::convertToSensorEvents( |
| 702 | const hidl_vec<Event> &src, |
| 703 | const hidl_vec<SensorInfo> &dynamicSensorsAdded, |
| 704 | sensors_event_t *dst) { |
| 705 | // Allocate a sensor_t structure for each dynamic sensor added and insert |
| 706 | // it into the dictionary of connected dynamic sensors keyed by handle. |
| 707 | for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) { |
| 708 | const SensorInfo &info = dynamicSensorsAdded[i]; |
| 709 | |
| 710 | auto it = mConnectedDynamicSensors.find(info.sensorHandle); |
| 711 | CHECK(it == mConnectedDynamicSensors.end()); |
| 712 | |
| 713 | sensor_t *sensor = new sensor_t; |
| 714 | convertToSensor(info, sensor); |
| 715 | |
| 716 | mConnectedDynamicSensors.insert( |
| 717 | std::make_pair(sensor->handle, sensor)); |
| 718 | } |
| 719 | |
| 720 | for (size_t i = 0; i < src.size(); ++i) { |
| 721 | convertToSensorEvent(src[i], &dst[i]); |
| 722 | } |
| 723 | } |
| 724 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 725 | void SensorDevice::handleHidlDeath(const std::string & detail) { |
| 726 | // restart is the only option at present. |
| 727 | LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str()); |
| 728 | } |
| 729 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 730 | // --------------------------------------------------------------------------- |
| 731 | }; // namespace android |