blob: 4ac9651a7bae111f027c21b09b3bdfaf3fab3e13 [file] [log] [blame]
Peng Xue36e3472016-11-03 11:57:10 -07001/*
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
Peng Xue36e3472016-11-03 11:57:10 -070017#include "SensorDirectConnection.h"
Mike Ma24743862020-01-29 00:36:55 -080018#include <android/util/ProtoOutputStream.h>
19#include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
Peng Xue36e3472016-11-03 11:57:10 -070020#include <hardware/sensors.h>
Brian Duddie0a4cebb2022-08-25 19:20:18 +000021#include "SensorDevice.h"
Peng Xue36e3472016-11-03 11:57:10 -070022
Peng Xue36e3472016-11-03 11:57:10 -070023#define UNUSED(x) (void)(x)
24
25namespace android {
26
Mike Ma24743862020-01-29 00:36:55 -080027using util::ProtoOutputStream;
28
Peng Xue36e3472016-11-03 11:57:10 -070029SensorService::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 Xu8cbefd72017-07-10 16:41:08 -070034 mOpPackageName(opPackageName), mDestroyed(false) {
Anh Pham5198c992021-02-10 14:15:30 +010035 mUserId = multiuser_get_user_id(mUid);
Peng Xue36e3472016-11-03 11:57:10 -070036 ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection");
37}
38
39SensorService::SensorDirectConnection::~SensorDirectConnection() {
40 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorDirectConnection %p", this);
Peng Xu8cbefd72017-07-10 16:41:08 -070041 destroy();
42}
43
44void SensorService::SensorDirectConnection::destroy() {
45 Mutex::Autolock _l(mDestroyLock);
46 // destroy once only
47 if (mDestroyed) {
48 return;
49 }
Peng Xue36e3472016-11-03 11:57:10 -070050
51 stopAll();
52 mService->cleanupConnection(this);
53 if (mMem.handle != nullptr) {
Brian Duddie0a4cebb2022-08-25 19:20:18 +000054 native_handle_close_with_tag(mMem.handle);
Peng Xue36e3472016-11-03 11:57:10 -070055 native_handle_delete(const_cast<struct native_handle*>(mMem.handle));
56 }
Peng Xu8cbefd72017-07-10 16:41:08 -070057 mDestroyed = true;
Peng Xue36e3472016-11-03 11:57:10 -070058}
59
60void SensorService::SensorDirectConnection::onFirstRef() {
61}
62
63void 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 Ma24743862020-01-29 00:36:55 -080072/**
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 */
79void 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 Xue36e3472016-11-03 11:57:10 -070093sp<BitTube> SensorService::SensorDirectConnection::getSensorChannel() const {
94 return nullptr;
95}
96
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070097void SensorService::SensorDirectConnection::onSensorAccessChanged(bool hasAccess) {
98 if (!hasAccess) {
99 stopAll(true /* backupRecord */);
100 } else {
101 recoverAll();
102 }
103}
104
Anh Pham5198c992021-02-10 14:15:30 +0100105void SensorService::SensorDirectConnection::onMicSensorAccessChanged(bool isMicToggleOn) {
106 if (isMicToggleOn) {
107 capRates();
108 } else {
109 uncapRates();
110 }
111}
112
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700113bool SensorService::SensorDirectConnection::hasSensorAccess() const {
114 return mService->hasSensorAccess(mUid, mOpPackageName);
115}
116
Peng Xue36e3472016-11-03 11:57:10 -0700117status_t SensorService::SensorDirectConnection::enableDisable(
118 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
119 int reservedFlags) {
120 // SensorDirectConnection does not support enableDisable, parameters not used
121 UNUSED(handle);
122 UNUSED(enabled);
123 UNUSED(samplingPeriodNs);
124 UNUSED(maxBatchReportLatencyNs);
125 UNUSED(reservedFlags);
126 return INVALID_OPERATION;
127}
128
129status_t SensorService::SensorDirectConnection::setEventRate(
130 int handle, nsecs_t samplingPeriodNs) {
131 // SensorDirectConnection does not support setEventRate, parameters not used
132 UNUSED(handle);
133 UNUSED(samplingPeriodNs);
134 return INVALID_OPERATION;
135}
136
137status_t SensorService::SensorDirectConnection::flush() {
138 // SensorDirectConnection does not support flush
139 return INVALID_OPERATION;
140}
141
142int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int rateLevel) {
143
144 if (handle == -1 && rateLevel == SENSOR_DIRECT_RATE_STOP) {
145 stopAll();
Anh Pham5198c992021-02-10 14:15:30 +0100146 mMicRateBackup.clear();
Peng Xue36e3472016-11-03 11:57:10 -0700147 return NO_ERROR;
148 }
149
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700150 if (!hasSensorAccess()) {
Peng Xue36e3472016-11-03 11:57:10 -0700151 return PERMISSION_DENIED;
152 }
153
Vladimir Komsiyski705e5ab2022-12-08 17:29:14 +0100154 std::shared_ptr<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
Peng Xue36e3472016-11-03 11:57:10 -0700155 if (si == nullptr) {
156 return NAME_NOT_FOUND;
157 }
158
159 const Sensor& s = si->getSensor();
Brian Duddie4a4d0462022-05-09 16:49:49 -0700160 if (!mService->canAccessSensor(s, "config direct channel", mOpPackageName)) {
Peng Xue36e3472016-11-03 11:57:10 -0700161 return PERMISSION_DENIED;
162 }
163
164 if (s.getHighestDirectReportRateLevel() == 0
165 || rateLevel > s.getHighestDirectReportRateLevel()
166 || !s.isDirectChannelTypeSupported(mMem.type)) {
167 return INVALID_OPERATION;
168 }
169
Anh Pham5198c992021-02-10 14:15:30 +0100170 int requestedRateLevel = rateLevel;
Anh Phamaf91a912021-02-10 14:10:53 +0100171 if (mService->isSensorInCappedSet(s.getType()) && rateLevel != SENSOR_DIRECT_RATE_STOP) {
172 status_t err = mService->adjustRateLevelBasedOnMicAndPermission(&rateLevel, mOpPackageName);
173 if (err != OK) {
174 return err;
175 }
176 }
177
Peng Xue36e3472016-11-03 11:57:10 -0700178 struct sensors_direct_cfg_t config = {
179 .rate_level = rateLevel
180 };
181
182 Mutex::Autolock _l(mConnectionLock);
183 SensorDevice& dev(SensorDevice::getInstance());
184 int ret = dev.configureDirectChannel(handle, getHalChannelHandle(), &config);
185
186 if (rateLevel == SENSOR_DIRECT_RATE_STOP) {
187 if (ret == NO_ERROR) {
188 mActivated.erase(handle);
Anh Pham5198c992021-02-10 14:15:30 +0100189 mMicRateBackup.erase(handle);
Peng Xue36e3472016-11-03 11:57:10 -0700190 } else if (ret > 0) {
191 ret = UNKNOWN_ERROR;
192 }
193 } else {
194 if (ret > 0) {
195 mActivated[handle] = rateLevel;
Anh Pham5198c992021-02-10 14:15:30 +0100196 if (mService->isSensorInCappedSet(s.getType())) {
197 // Back up the rates that the app is allowed to have if the mic toggle is off
198 // This is used in the uncapRates() function.
Arthur Ishiguro8a628522021-09-22 14:10:16 -0700199 if ((requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) ||
200 !isRateCappedBasedOnPermission()) {
Anh Pham5198c992021-02-10 14:15:30 +0100201 mMicRateBackup[handle] = requestedRateLevel;
202 } else {
203 mMicRateBackup[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
204 }
205 }
Peng Xue36e3472016-11-03 11:57:10 -0700206 }
207 }
208
209 return ret;
210}
211
Anh Pham5198c992021-02-10 14:15:30 +0100212void SensorService::SensorDirectConnection::capRates() {
213 Mutex::Autolock _l(mConnectionLock);
214 const struct sensors_direct_cfg_t capConfig = {
215 .rate_level = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL
216 };
217
218 const struct sensors_direct_cfg_t stopConfig = {
219 .rate_level = SENSOR_DIRECT_RATE_STOP
220 };
221
222 // If our requests are in the backup, then we shouldn't activate sensors from here
223 bool temporarilyStopped = mActivated.empty() && !mActivatedBackup.empty();
224 std::unordered_map<int, int>& existingConnections =
225 (!temporarilyStopped) ? mActivated : mActivatedBackup;
226
227 SensorDevice& dev(SensorDevice::getInstance());
228 for (auto &i : existingConnections) {
229 int handle = i.first;
230 int rateLevel = i.second;
Vladimir Komsiyski705e5ab2022-12-08 17:29:14 +0100231 std::shared_ptr<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
Anh Pham5198c992021-02-10 14:15:30 +0100232 if (si != nullptr) {
233 const Sensor& s = si->getSensor();
234 if (mService->isSensorInCappedSet(s.getType()) &&
235 rateLevel > SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) {
236 mMicRateBackup[handle] = rateLevel;
237 // Modify the rate kept by the existing map
238 existingConnections[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
239 // Only reconfigure the channel if it's ongoing
240 if (!temporarilyStopped) {
241 // Stopping before reconfiguring is the well-tested path in CTS
242 dev.configureDirectChannel(handle, getHalChannelHandle(), &stopConfig);
243 dev.configureDirectChannel(handle, getHalChannelHandle(), &capConfig);
244 }
245 }
246 }
247 }
248}
249
250void SensorService::SensorDirectConnection::uncapRates() {
251 Mutex::Autolock _l(mConnectionLock);
252
253 // If our requests are in the backup, then we shouldn't activate sensors from here
254 bool temporarilyStopped = mActivated.empty() && !mActivatedBackup.empty();
255 std::unordered_map<int, int>& existingConnections =
256 (!temporarilyStopped) ? mActivated : mActivatedBackup;
257
258 const struct sensors_direct_cfg_t stopConfig = {
259 .rate_level = SENSOR_DIRECT_RATE_STOP
260 };
261 SensorDevice& dev(SensorDevice::getInstance());
262 for (auto &i : mMicRateBackup) {
263 int handle = i.first;
264 int rateLevel = i.second;
265
266 const struct sensors_direct_cfg_t config = {
267 .rate_level = rateLevel
268 };
269
270 // Modify the rate kept by the existing map
271 existingConnections[handle] = rateLevel;
272
273 // Only reconfigure the channel if it's ongoing
274 if (!temporarilyStopped) {
275 // Stopping before reconfiguring is the well-tested path in CTS
276 dev.configureDirectChannel(handle, getHalChannelHandle(), &stopConfig);
277 dev.configureDirectChannel(handle, getHalChannelHandle(), &config);
278 }
279 }
280 mMicRateBackup.clear();
281}
282
Peng Xue36e3472016-11-03 11:57:10 -0700283void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700284 Mutex::Autolock _l(mConnectionLock);
285 stopAllLocked(backupRecord);
286}
Peng Xue36e3472016-11-03 11:57:10 -0700287
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700288void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) {
Peng Xue36e3472016-11-03 11:57:10 -0700289 struct sensors_direct_cfg_t config = {
290 .rate_level = SENSOR_DIRECT_RATE_STOP
291 };
292
Peng Xue36e3472016-11-03 11:57:10 -0700293 SensorDevice& dev(SensorDevice::getInstance());
294 for (auto &i : mActivated) {
295 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
296 }
297
298 if (backupRecord && mActivatedBackup.empty()) {
299 mActivatedBackup = mActivated;
300 }
301 mActivated.clear();
302}
303
304void SensorService::SensorDirectConnection::recoverAll() {
Peng Xue36e3472016-11-03 11:57:10 -0700305 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700306 if (!mActivatedBackup.empty()) {
307 stopAllLocked(false);
Peng Xue36e3472016-11-03 11:57:10 -0700308
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700309 SensorDevice& dev(SensorDevice::getInstance());
Peng Xue36e3472016-11-03 11:57:10 -0700310
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700311 // recover list of report from backup
312 ALOG_ASSERT(mActivated.empty(),
313 "mActivated must be empty if mActivatedBackup was non-empty");
314 mActivated = mActivatedBackup;
315 mActivatedBackup.clear();
316
317 // re-enable them
318 for (auto &i : mActivated) {
319 struct sensors_direct_cfg_t config = {
320 .rate_level = i.second
321 };
322 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
323 }
Peng Xue36e3472016-11-03 11:57:10 -0700324 }
325}
326
327int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
328 return mHalChannelHandle;
329}
330
331bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const {
332 bool ret = false;
333
334 if (mMem.type == mem->type) {
335 switch (mMem.type) {
336 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
Peng Xu36407732017-05-16 15:12:22 -0700337 // there is no known method to test if two ashmem fds are equivalent besides
338 // trivially comparing the fd values (ino number from fstat() are always the
339 // same, pointing to "/dev/ashmem").
340 int fd1 = mMem.handle->data[0];
341 int fd2 = mem->handle->data[0];
342 ret = (fd1 == fd2);
Peng Xuf149b402017-02-09 12:24:25 -0800343 break;
Peng Xue36e3472016-11-03 11:57:10 -0700344 }
345 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700346 // there is no known method to test if two gralloc handle are equivalent
347 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800348 break;
Peng Xue36e3472016-11-03 11:57:10 -0700349 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700350 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700351 ALOGE("Unexpected mem type %d", mMem.type);
352 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800353 break;
Peng Xue36e3472016-11-03 11:57:10 -0700354 }
355 }
356 return ret;
357}
358
359} // namespace android
360