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