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