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