blob: 33724a93b58633e56139061ad9f90ede3c602c9c [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
Rocky Fang8aa09802024-08-01 18:07:54 +000029SensorService::SensorDirectConnection::SensorDirectConnection(
30 const sp<SensorService>& service, uid_t uid, pid_t pid, const sensors_direct_mem_t* mem,
31 int32_t halChannelHandle, const String16& opPackageName, int deviceId)
32 : mService(service),
33 mUid(uid),
34 mPid(pid),
35 mMem(*mem),
Peng Xue36e3472016-11-03 11:57:10 -070036 mHalChannelHandle(halChannelHandle),
Rocky Fang8aa09802024-08-01 18:07:54 +000037 mOpPackageName(opPackageName),
38 mDeviceId(deviceId),
39 mDestroyed(false) {
Anh Pham5198c992021-02-10 14:15:30 +010040 mUserId = multiuser_get_user_id(mUid);
Peng Xue36e3472016-11-03 11:57:10 -070041 ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection");
42}
43
44SensorService::SensorDirectConnection::~SensorDirectConnection() {
45 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorDirectConnection %p", this);
Peng Xu8cbefd72017-07-10 16:41:08 -070046 destroy();
47}
48
49void SensorService::SensorDirectConnection::destroy() {
50 Mutex::Autolock _l(mDestroyLock);
51 // destroy once only
52 if (mDestroyed) {
53 return;
54 }
Peng Xue36e3472016-11-03 11:57:10 -070055
56 stopAll();
57 mService->cleanupConnection(this);
58 if (mMem.handle != nullptr) {
Brian Duddie0a4cebb2022-08-25 19:20:18 +000059 native_handle_close_with_tag(mMem.handle);
Peng Xue36e3472016-11-03 11:57:10 -070060 native_handle_delete(const_cast<struct native_handle*>(mMem.handle));
61 }
Peng Xu8cbefd72017-07-10 16:41:08 -070062 mDestroyed = true;
Peng Xue36e3472016-11-03 11:57:10 -070063}
64
65void SensorService::SensorDirectConnection::onFirstRef() {
66}
67
68void SensorService::SensorDirectConnection::dump(String8& result) const {
69 Mutex::Autolock _l(mConnectionLock);
Rocky Fang8aa09802024-08-01 18:07:54 +000070 result.appendFormat("\t%s | HAL channel handle %d | uid %d | pid %d\n",
71 String8(mOpPackageName).c_str(), getHalChannelHandle(), mUid, mPid);
72 result.appendFormat("\tActivated sensor count: %zu\n", mActivated.size());
73 dumpSensorInfoWithLock(result, mActivated);
74
75 result.appendFormat("\tBackup sensor (opened but UID idle) count: %zu\n",
76 mActivatedBackup.size());
77 dumpSensorInfoWithLock(result, mActivatedBackup);
78}
79
80void SensorService::SensorDirectConnection::dumpSensorInfoWithLock(
81 String8& result, std::unordered_map<int, int> sensors) const {
82 for (auto& i : sensors) {
83 result.appendFormat("\t\t%s 0x%08x | rate %d\n", mService->getSensorName(i.first).c_str(),
84 i.first, i.second);
Peng Xue36e3472016-11-03 11:57:10 -070085 }
86}
87
Mike Ma24743862020-01-29 00:36:55 -080088/**
89 * Dump debugging information as android.service.SensorDirectConnectionProto protobuf message using
90 * ProtoOutputStream.
91 *
92 * See proto definition and some notes about ProtoOutputStream in
93 * frameworks/base/core/proto/android/service/sensor_service.proto
94 */
95void SensorService::SensorDirectConnection::dump(ProtoOutputStream* proto) const {
96 using namespace service::SensorDirectConnectionProto;
97 Mutex::Autolock _l(mConnectionLock);
Tomasz Wasilczyk83aa0ab2023-08-11 00:06:51 +000098 proto->write(PACKAGE_NAME, std::string(String8(mOpPackageName).c_str()));
Mike Ma24743862020-01-29 00:36:55 -080099 proto->write(HAL_CHANNEL_HANDLE, getHalChannelHandle());
100 proto->write(NUM_SENSOR_ACTIVATED, int(mActivated.size()));
101 for (auto &i : mActivated) {
102 uint64_t token = proto->start(SENSORS);
103 proto->write(SensorProto::SENSOR, i.first);
104 proto->write(SensorProto::RATE, i.second);
105 proto->end(token);
106 }
107}
108
Peng Xue36e3472016-11-03 11:57:10 -0700109sp<BitTube> SensorService::SensorDirectConnection::getSensorChannel() const {
110 return nullptr;
111}
112
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700113void SensorService::SensorDirectConnection::onSensorAccessChanged(bool hasAccess) {
114 if (!hasAccess) {
115 stopAll(true /* backupRecord */);
116 } else {
117 recoverAll();
118 }
119}
120
Anh Pham5198c992021-02-10 14:15:30 +0100121void SensorService::SensorDirectConnection::onMicSensorAccessChanged(bool isMicToggleOn) {
122 if (isMicToggleOn) {
123 capRates();
124 } else {
125 uncapRates();
126 }
127}
128
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700129bool SensorService::SensorDirectConnection::hasSensorAccess() const {
130 return mService->hasSensorAccess(mUid, mOpPackageName);
131}
132
Peng Xue36e3472016-11-03 11:57:10 -0700133status_t SensorService::SensorDirectConnection::enableDisable(
134 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
135 int reservedFlags) {
136 // SensorDirectConnection does not support enableDisable, parameters not used
137 UNUSED(handle);
138 UNUSED(enabled);
139 UNUSED(samplingPeriodNs);
140 UNUSED(maxBatchReportLatencyNs);
141 UNUSED(reservedFlags);
142 return INVALID_OPERATION;
143}
144
145status_t SensorService::SensorDirectConnection::setEventRate(
146 int handle, nsecs_t samplingPeriodNs) {
147 // SensorDirectConnection does not support setEventRate, parameters not used
148 UNUSED(handle);
149 UNUSED(samplingPeriodNs);
150 return INVALID_OPERATION;
151}
152
153status_t SensorService::SensorDirectConnection::flush() {
154 // SensorDirectConnection does not support flush
155 return INVALID_OPERATION;
156}
157
158int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int rateLevel) {
159
160 if (handle == -1 && rateLevel == SENSOR_DIRECT_RATE_STOP) {
161 stopAll();
Anh Pham5198c992021-02-10 14:15:30 +0100162 mMicRateBackup.clear();
Peng Xue36e3472016-11-03 11:57:10 -0700163 return NO_ERROR;
164 }
165
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700166 if (!hasSensorAccess()) {
Peng Xue36e3472016-11-03 11:57:10 -0700167 return PERMISSION_DENIED;
168 }
169
Vladimir Komsiyski705e5ab2022-12-08 17:29:14 +0100170 std::shared_ptr<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
Peng Xue36e3472016-11-03 11:57:10 -0700171 if (si == nullptr) {
172 return NAME_NOT_FOUND;
173 }
174
175 const Sensor& s = si->getSensor();
Brian Duddie4a4d0462022-05-09 16:49:49 -0700176 if (!mService->canAccessSensor(s, "config direct channel", mOpPackageName)) {
Peng Xue36e3472016-11-03 11:57:10 -0700177 return PERMISSION_DENIED;
178 }
179
180 if (s.getHighestDirectReportRateLevel() == 0
181 || rateLevel > s.getHighestDirectReportRateLevel()
182 || !s.isDirectChannelTypeSupported(mMem.type)) {
183 return INVALID_OPERATION;
184 }
185
Anh Pham5198c992021-02-10 14:15:30 +0100186 int requestedRateLevel = rateLevel;
Anh Phamaf91a912021-02-10 14:10:53 +0100187 if (mService->isSensorInCappedSet(s.getType()) && rateLevel != SENSOR_DIRECT_RATE_STOP) {
188 status_t err = mService->adjustRateLevelBasedOnMicAndPermission(&rateLevel, mOpPackageName);
189 if (err != OK) {
190 return err;
191 }
192 }
193
Peng Xue36e3472016-11-03 11:57:10 -0700194 struct sensors_direct_cfg_t config = {
195 .rate_level = rateLevel
196 };
197
198 Mutex::Autolock _l(mConnectionLock);
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100199 int ret = configure(handle, &config);
Peng Xue36e3472016-11-03 11:57:10 -0700200
201 if (rateLevel == SENSOR_DIRECT_RATE_STOP) {
202 if (ret == NO_ERROR) {
203 mActivated.erase(handle);
Anh Pham5198c992021-02-10 14:15:30 +0100204 mMicRateBackup.erase(handle);
Peng Xue36e3472016-11-03 11:57:10 -0700205 } else if (ret > 0) {
206 ret = UNKNOWN_ERROR;
207 }
208 } else {
209 if (ret > 0) {
210 mActivated[handle] = rateLevel;
Anh Pham5198c992021-02-10 14:15:30 +0100211 if (mService->isSensorInCappedSet(s.getType())) {
212 // Back up the rates that the app is allowed to have if the mic toggle is off
213 // This is used in the uncapRates() function.
Arthur Ishiguro8a628522021-09-22 14:10:16 -0700214 if ((requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) ||
215 !isRateCappedBasedOnPermission()) {
Anh Pham5198c992021-02-10 14:15:30 +0100216 mMicRateBackup[handle] = requestedRateLevel;
217 } else {
218 mMicRateBackup[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
219 }
220 }
Peng Xue36e3472016-11-03 11:57:10 -0700221 }
222 }
223
224 return ret;
225}
226
Anh Pham5198c992021-02-10 14:15:30 +0100227void SensorService::SensorDirectConnection::capRates() {
228 Mutex::Autolock _l(mConnectionLock);
229 const struct sensors_direct_cfg_t capConfig = {
230 .rate_level = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL
231 };
232
233 const struct sensors_direct_cfg_t stopConfig = {
234 .rate_level = SENSOR_DIRECT_RATE_STOP
235 };
236
237 // If our requests are in the backup, then we shouldn't activate sensors from here
238 bool temporarilyStopped = mActivated.empty() && !mActivatedBackup.empty();
239 std::unordered_map<int, int>& existingConnections =
240 (!temporarilyStopped) ? mActivated : mActivatedBackup;
241
Anh Pham5198c992021-02-10 14:15:30 +0100242 for (auto &i : existingConnections) {
243 int handle = i.first;
244 int rateLevel = i.second;
Vladimir Komsiyski705e5ab2022-12-08 17:29:14 +0100245 std::shared_ptr<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
Anh Pham5198c992021-02-10 14:15:30 +0100246 if (si != nullptr) {
247 const Sensor& s = si->getSensor();
248 if (mService->isSensorInCappedSet(s.getType()) &&
249 rateLevel > SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) {
250 mMicRateBackup[handle] = rateLevel;
251 // Modify the rate kept by the existing map
252 existingConnections[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
253 // Only reconfigure the channel if it's ongoing
254 if (!temporarilyStopped) {
255 // Stopping before reconfiguring is the well-tested path in CTS
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100256 configure(handle, &stopConfig);
257 configure(handle, &capConfig);
Anh Pham5198c992021-02-10 14:15:30 +0100258 }
259 }
260 }
261 }
262}
263
264void SensorService::SensorDirectConnection::uncapRates() {
265 Mutex::Autolock _l(mConnectionLock);
266
267 // If our requests are in the backup, then we shouldn't activate sensors from here
268 bool temporarilyStopped = mActivated.empty() && !mActivatedBackup.empty();
269 std::unordered_map<int, int>& existingConnections =
270 (!temporarilyStopped) ? mActivated : mActivatedBackup;
271
272 const struct sensors_direct_cfg_t stopConfig = {
273 .rate_level = SENSOR_DIRECT_RATE_STOP
274 };
Anh Pham5198c992021-02-10 14:15:30 +0100275 for (auto &i : mMicRateBackup) {
276 int handle = i.first;
277 int rateLevel = i.second;
278
279 const struct sensors_direct_cfg_t config = {
280 .rate_level = rateLevel
281 };
282
283 // Modify the rate kept by the existing map
284 existingConnections[handle] = rateLevel;
285
286 // Only reconfigure the channel if it's ongoing
287 if (!temporarilyStopped) {
288 // Stopping before reconfiguring is the well-tested path in CTS
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100289 configure(handle, &stopConfig);
290 configure(handle, &config);
Anh Pham5198c992021-02-10 14:15:30 +0100291 }
292 }
293 mMicRateBackup.clear();
294}
295
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100296int SensorService::SensorDirectConnection::configure(
297 int handle, const sensors_direct_cfg_t* config) {
298 if (mDeviceId == RuntimeSensor::DEFAULT_DEVICE_ID) {
299 SensorDevice& dev(SensorDevice::getInstance());
300 return dev.configureDirectChannel(handle, getHalChannelHandle(), config);
301 } else {
302 return mService->configureRuntimeSensorDirectChannel(handle, this, config);
303 }
304}
305
Peng Xue36e3472016-11-03 11:57:10 -0700306void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700307 Mutex::Autolock _l(mConnectionLock);
308 stopAllLocked(backupRecord);
309}
Peng Xue36e3472016-11-03 11:57:10 -0700310
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700311void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) {
Peng Xue36e3472016-11-03 11:57:10 -0700312 struct sensors_direct_cfg_t config = {
313 .rate_level = SENSOR_DIRECT_RATE_STOP
314 };
315
Peng Xue36e3472016-11-03 11:57:10 -0700316 for (auto &i : mActivated) {
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100317 configure(i.first, &config);
Peng Xue36e3472016-11-03 11:57:10 -0700318 }
319
320 if (backupRecord && mActivatedBackup.empty()) {
321 mActivatedBackup = mActivated;
322 }
323 mActivated.clear();
324}
325
326void SensorService::SensorDirectConnection::recoverAll() {
Peng Xue36e3472016-11-03 11:57:10 -0700327 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700328 if (!mActivatedBackup.empty()) {
329 stopAllLocked(false);
Peng Xue36e3472016-11-03 11:57:10 -0700330
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700331 // recover list of report from backup
332 ALOG_ASSERT(mActivated.empty(),
333 "mActivated must be empty if mActivatedBackup was non-empty");
334 mActivated = mActivatedBackup;
335 mActivatedBackup.clear();
336
337 // re-enable them
338 for (auto &i : mActivated) {
339 struct sensors_direct_cfg_t config = {
340 .rate_level = i.second
341 };
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100342 configure(i.first, &config);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700343 }
Peng Xue36e3472016-11-03 11:57:10 -0700344 }
345}
346
347int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
348 return mHalChannelHandle;
349}
350
351bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const {
352 bool ret = false;
353
354 if (mMem.type == mem->type) {
355 switch (mMem.type) {
356 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
Peng Xu36407732017-05-16 15:12:22 -0700357 // there is no known method to test if two ashmem fds are equivalent besides
358 // trivially comparing the fd values (ino number from fstat() are always the
359 // same, pointing to "/dev/ashmem").
360 int fd1 = mMem.handle->data[0];
361 int fd2 = mem->handle->data[0];
362 ret = (fd1 == fd2);
Peng Xuf149b402017-02-09 12:24:25 -0800363 break;
Peng Xue36e3472016-11-03 11:57:10 -0700364 }
365 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700366 // there is no known method to test if two gralloc handle are equivalent
367 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800368 break;
Peng Xue36e3472016-11-03 11:57:10 -0700369 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700370 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700371 ALOGE("Unexpected mem type %d", mMem.type);
372 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800373 break;
Peng Xue36e3472016-11-03 11:57:10 -0700374 }
375 }
376 return ret;
377}
378
379} // namespace android
380