Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
| 16 | |
| 17 | #include "SensorDevice.h" |
| 18 | #include "SensorDirectConnection.h" |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 19 | #include <android/util/ProtoOutputStream.h> |
| 20 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 21 | #include <hardware/sensors.h> |
| 22 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 23 | #define UNUSED(x) (void)(x) |
| 24 | |
| 25 | namespace android { |
| 26 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 27 | using util::ProtoOutputStream; |
| 28 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 29 | SensorService::SensorDirectConnection::SensorDirectConnection(const sp<SensorService>& service, |
| 30 | uid_t uid, const sensors_direct_mem_t *mem, int32_t halChannelHandle, |
| 31 | const String16& opPackageName) |
| 32 | : mService(service), mUid(uid), mMem(*mem), |
| 33 | mHalChannelHandle(halChannelHandle), |
Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 34 | mOpPackageName(opPackageName), mDestroyed(false) { |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame^] | 35 | mIsRateCappedBasedOnPermission = mService->isRateCappedBasedOnPermission(mOpPackageName); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 36 | ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection"); |
| 37 | } |
| 38 | |
| 39 | SensorService::SensorDirectConnection::~SensorDirectConnection() { |
| 40 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorDirectConnection %p", this); |
Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 41 | destroy(); |
| 42 | } |
| 43 | |
| 44 | void SensorService::SensorDirectConnection::destroy() { |
| 45 | Mutex::Autolock _l(mDestroyLock); |
| 46 | // destroy once only |
| 47 | if (mDestroyed) { |
| 48 | return; |
| 49 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 50 | |
| 51 | stopAll(); |
| 52 | mService->cleanupConnection(this); |
| 53 | if (mMem.handle != nullptr) { |
| 54 | native_handle_close(mMem.handle); |
| 55 | native_handle_delete(const_cast<struct native_handle*>(mMem.handle)); |
| 56 | } |
Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 57 | mDestroyed = true; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void SensorService::SensorDirectConnection::onFirstRef() { |
| 61 | } |
| 62 | |
| 63 | void SensorService::SensorDirectConnection::dump(String8& result) const { |
| 64 | Mutex::Autolock _l(mConnectionLock); |
| 65 | result.appendFormat("\tPackage %s, HAL channel handle %d, total sensor activated %zu\n", |
| 66 | String8(mOpPackageName).string(), getHalChannelHandle(), mActivated.size()); |
| 67 | for (auto &i : mActivated) { |
| 68 | result.appendFormat("\t\tSensor %#08x, rate %d\n", i.first, i.second); |
| 69 | } |
| 70 | } |
| 71 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Dump debugging information as android.service.SensorDirectConnectionProto protobuf message using |
| 74 | * ProtoOutputStream. |
| 75 | * |
| 76 | * See proto definition and some notes about ProtoOutputStream in |
| 77 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 78 | */ |
| 79 | void SensorService::SensorDirectConnection::dump(ProtoOutputStream* proto) const { |
| 80 | using namespace service::SensorDirectConnectionProto; |
| 81 | Mutex::Autolock _l(mConnectionLock); |
| 82 | proto->write(PACKAGE_NAME, std::string(String8(mOpPackageName).string())); |
| 83 | proto->write(HAL_CHANNEL_HANDLE, getHalChannelHandle()); |
| 84 | proto->write(NUM_SENSOR_ACTIVATED, int(mActivated.size())); |
| 85 | for (auto &i : mActivated) { |
| 86 | uint64_t token = proto->start(SENSORS); |
| 87 | proto->write(SensorProto::SENSOR, i.first); |
| 88 | proto->write(SensorProto::RATE, i.second); |
| 89 | proto->end(token); |
| 90 | } |
| 91 | } |
| 92 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 93 | sp<BitTube> SensorService::SensorDirectConnection::getSensorChannel() const { |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 97 | void SensorService::SensorDirectConnection::onSensorAccessChanged(bool hasAccess) { |
| 98 | if (!hasAccess) { |
| 99 | stopAll(true /* backupRecord */); |
| 100 | } else { |
| 101 | recoverAll(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | bool SensorService::SensorDirectConnection::hasSensorAccess() const { |
| 106 | return mService->hasSensorAccess(mUid, mOpPackageName); |
| 107 | } |
| 108 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 109 | status_t SensorService::SensorDirectConnection::enableDisable( |
| 110 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, |
| 111 | int reservedFlags) { |
| 112 | // SensorDirectConnection does not support enableDisable, parameters not used |
| 113 | UNUSED(handle); |
| 114 | UNUSED(enabled); |
| 115 | UNUSED(samplingPeriodNs); |
| 116 | UNUSED(maxBatchReportLatencyNs); |
| 117 | UNUSED(reservedFlags); |
| 118 | return INVALID_OPERATION; |
| 119 | } |
| 120 | |
| 121 | status_t SensorService::SensorDirectConnection::setEventRate( |
| 122 | int handle, nsecs_t samplingPeriodNs) { |
| 123 | // SensorDirectConnection does not support setEventRate, parameters not used |
| 124 | UNUSED(handle); |
| 125 | UNUSED(samplingPeriodNs); |
| 126 | return INVALID_OPERATION; |
| 127 | } |
| 128 | |
| 129 | status_t SensorService::SensorDirectConnection::flush() { |
| 130 | // SensorDirectConnection does not support flush |
| 131 | return INVALID_OPERATION; |
| 132 | } |
| 133 | |
| 134 | int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int rateLevel) { |
| 135 | |
| 136 | if (handle == -1 && rateLevel == SENSOR_DIRECT_RATE_STOP) { |
| 137 | stopAll(); |
| 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 141 | if (!hasSensorAccess()) { |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 142 | return PERMISSION_DENIED; |
| 143 | } |
| 144 | |
| 145 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle); |
| 146 | if (si == nullptr) { |
| 147 | return NAME_NOT_FOUND; |
| 148 | } |
| 149 | |
| 150 | const Sensor& s = si->getSensor(); |
| 151 | if (!SensorService::canAccessSensor(s, "config direct channel", mOpPackageName)) { |
| 152 | return PERMISSION_DENIED; |
| 153 | } |
| 154 | |
| 155 | if (s.getHighestDirectReportRateLevel() == 0 |
| 156 | || rateLevel > s.getHighestDirectReportRateLevel() |
| 157 | || !s.isDirectChannelTypeSupported(mMem.type)) { |
| 158 | return INVALID_OPERATION; |
| 159 | } |
| 160 | |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame^] | 161 | if (mService->isSensorInCappedSet(s.getType()) && rateLevel != SENSOR_DIRECT_RATE_STOP) { |
| 162 | status_t err = mService->adjustRateLevelBasedOnMicAndPermission(&rateLevel, mOpPackageName); |
| 163 | if (err != OK) { |
| 164 | return err; |
| 165 | } |
| 166 | } |
| 167 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 168 | struct sensors_direct_cfg_t config = { |
| 169 | .rate_level = rateLevel |
| 170 | }; |
| 171 | |
| 172 | Mutex::Autolock _l(mConnectionLock); |
| 173 | SensorDevice& dev(SensorDevice::getInstance()); |
| 174 | int ret = dev.configureDirectChannel(handle, getHalChannelHandle(), &config); |
| 175 | |
| 176 | if (rateLevel == SENSOR_DIRECT_RATE_STOP) { |
| 177 | if (ret == NO_ERROR) { |
| 178 | mActivated.erase(handle); |
| 179 | } else if (ret > 0) { |
| 180 | ret = UNKNOWN_ERROR; |
| 181 | } |
| 182 | } else { |
| 183 | if (ret > 0) { |
| 184 | mActivated[handle] = rateLevel; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | void SensorService::SensorDirectConnection::stopAll(bool backupRecord) { |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 192 | Mutex::Autolock _l(mConnectionLock); |
| 193 | stopAllLocked(backupRecord); |
| 194 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 195 | |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 196 | void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) { |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 197 | struct sensors_direct_cfg_t config = { |
| 198 | .rate_level = SENSOR_DIRECT_RATE_STOP |
| 199 | }; |
| 200 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 201 | SensorDevice& dev(SensorDevice::getInstance()); |
| 202 | for (auto &i : mActivated) { |
| 203 | dev.configureDirectChannel(i.first, getHalChannelHandle(), &config); |
| 204 | } |
| 205 | |
| 206 | if (backupRecord && mActivatedBackup.empty()) { |
| 207 | mActivatedBackup = mActivated; |
| 208 | } |
| 209 | mActivated.clear(); |
| 210 | } |
| 211 | |
| 212 | void SensorService::SensorDirectConnection::recoverAll() { |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 213 | Mutex::Autolock _l(mConnectionLock); |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 214 | if (!mActivatedBackup.empty()) { |
| 215 | stopAllLocked(false); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 216 | |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 217 | SensorDevice& dev(SensorDevice::getInstance()); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 218 | |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 219 | // recover list of report from backup |
| 220 | ALOG_ASSERT(mActivated.empty(), |
| 221 | "mActivated must be empty if mActivatedBackup was non-empty"); |
| 222 | mActivated = mActivatedBackup; |
| 223 | mActivatedBackup.clear(); |
| 224 | |
| 225 | // re-enable them |
| 226 | for (auto &i : mActivated) { |
| 227 | struct sensors_direct_cfg_t config = { |
| 228 | .rate_level = i.second |
| 229 | }; |
| 230 | dev.configureDirectChannel(i.first, getHalChannelHandle(), &config); |
| 231 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const { |
| 236 | return mHalChannelHandle; |
| 237 | } |
| 238 | |
| 239 | bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const { |
| 240 | bool ret = false; |
| 241 | |
| 242 | if (mMem.type == mem->type) { |
| 243 | switch (mMem.type) { |
| 244 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: { |
Peng Xu | 3640773 | 2017-05-16 15:12:22 -0700 | [diff] [blame] | 245 | // there is no known method to test if two ashmem fds are equivalent besides |
| 246 | // trivially comparing the fd values (ino number from fstat() are always the |
| 247 | // same, pointing to "/dev/ashmem"). |
| 248 | int fd1 = mMem.handle->data[0]; |
| 249 | int fd2 = mem->handle->data[0]; |
| 250 | ret = (fd1 == fd2); |
Peng Xu | f149b40 | 2017-02-09 12:24:25 -0800 | [diff] [blame] | 251 | break; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 252 | } |
| 253 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: |
Peng Xu | f88e2b9 | 2017-04-10 15:52:58 -0700 | [diff] [blame] | 254 | // there is no known method to test if two gralloc handle are equivalent |
| 255 | ret = false; |
Peng Xu | f149b40 | 2017-02-09 12:24:25 -0800 | [diff] [blame] | 256 | break; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 257 | default: |
Peng Xu | f88e2b9 | 2017-04-10 15:52:58 -0700 | [diff] [blame] | 258 | // should never happen |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 259 | ALOGE("Unexpected mem type %d", mMem.type); |
| 260 | ret = true; |
Peng Xu | f149b40 | 2017-02-09 12:24:25 -0800 | [diff] [blame] | 261 | break; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | } // namespace android |
| 268 | |