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