blob: 4fff8bb1b4e27a4d97555b6bd28ff201395f5134 [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,
Vladimir Komsiyski4871f092023-01-19 18:25:43 +010031 const String16& opPackageName, int deviceId)
Peng Xue36e3472016-11-03 11:57:10 -070032 : mService(service), mUid(uid), mMem(*mem),
33 mHalChannelHandle(halChannelHandle),
Vladimir Komsiyski4871f092023-01-19 18:25:43 +010034 mOpPackageName(opPackageName), mDeviceId(deviceId), 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);
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100183 int ret = configure(handle, &config);
Peng Xue36e3472016-11-03 11:57:10 -0700184
185 if (rateLevel == SENSOR_DIRECT_RATE_STOP) {
186 if (ret == NO_ERROR) {
187 mActivated.erase(handle);
Anh Pham5198c992021-02-10 14:15:30 +0100188 mMicRateBackup.erase(handle);
Peng Xue36e3472016-11-03 11:57:10 -0700189 } else if (ret > 0) {
190 ret = UNKNOWN_ERROR;
191 }
192 } else {
193 if (ret > 0) {
194 mActivated[handle] = rateLevel;
Anh Pham5198c992021-02-10 14:15:30 +0100195 if (mService->isSensorInCappedSet(s.getType())) {
196 // Back up the rates that the app is allowed to have if the mic toggle is off
197 // This is used in the uncapRates() function.
Arthur Ishiguro8a628522021-09-22 14:10:16 -0700198 if ((requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) ||
199 !isRateCappedBasedOnPermission()) {
Anh Pham5198c992021-02-10 14:15:30 +0100200 mMicRateBackup[handle] = requestedRateLevel;
201 } else {
202 mMicRateBackup[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
203 }
204 }
Peng Xue36e3472016-11-03 11:57:10 -0700205 }
206 }
207
208 return ret;
209}
210
Anh Pham5198c992021-02-10 14:15:30 +0100211void SensorService::SensorDirectConnection::capRates() {
212 Mutex::Autolock _l(mConnectionLock);
213 const struct sensors_direct_cfg_t capConfig = {
214 .rate_level = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL
215 };
216
217 const struct sensors_direct_cfg_t stopConfig = {
218 .rate_level = SENSOR_DIRECT_RATE_STOP
219 };
220
221 // If our requests are in the backup, then we shouldn't activate sensors from here
222 bool temporarilyStopped = mActivated.empty() && !mActivatedBackup.empty();
223 std::unordered_map<int, int>& existingConnections =
224 (!temporarilyStopped) ? mActivated : mActivatedBackup;
225
Anh Pham5198c992021-02-10 14:15:30 +0100226 for (auto &i : existingConnections) {
227 int handle = i.first;
228 int rateLevel = i.second;
Vladimir Komsiyski705e5ab2022-12-08 17:29:14 +0100229 std::shared_ptr<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
Anh Pham5198c992021-02-10 14:15:30 +0100230 if (si != nullptr) {
231 const Sensor& s = si->getSensor();
232 if (mService->isSensorInCappedSet(s.getType()) &&
233 rateLevel > SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) {
234 mMicRateBackup[handle] = rateLevel;
235 // Modify the rate kept by the existing map
236 existingConnections[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
237 // Only reconfigure the channel if it's ongoing
238 if (!temporarilyStopped) {
239 // Stopping before reconfiguring is the well-tested path in CTS
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100240 configure(handle, &stopConfig);
241 configure(handle, &capConfig);
Anh Pham5198c992021-02-10 14:15:30 +0100242 }
243 }
244 }
245 }
246}
247
248void SensorService::SensorDirectConnection::uncapRates() {
249 Mutex::Autolock _l(mConnectionLock);
250
251 // If our requests are in the backup, then we shouldn't activate sensors from here
252 bool temporarilyStopped = mActivated.empty() && !mActivatedBackup.empty();
253 std::unordered_map<int, int>& existingConnections =
254 (!temporarilyStopped) ? mActivated : mActivatedBackup;
255
256 const struct sensors_direct_cfg_t stopConfig = {
257 .rate_level = SENSOR_DIRECT_RATE_STOP
258 };
Anh Pham5198c992021-02-10 14:15:30 +0100259 for (auto &i : mMicRateBackup) {
260 int handle = i.first;
261 int rateLevel = i.second;
262
263 const struct sensors_direct_cfg_t config = {
264 .rate_level = rateLevel
265 };
266
267 // Modify the rate kept by the existing map
268 existingConnections[handle] = rateLevel;
269
270 // Only reconfigure the channel if it's ongoing
271 if (!temporarilyStopped) {
272 // Stopping before reconfiguring is the well-tested path in CTS
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100273 configure(handle, &stopConfig);
274 configure(handle, &config);
Anh Pham5198c992021-02-10 14:15:30 +0100275 }
276 }
277 mMicRateBackup.clear();
278}
279
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100280int SensorService::SensorDirectConnection::configure(
281 int handle, const sensors_direct_cfg_t* config) {
282 if (mDeviceId == RuntimeSensor::DEFAULT_DEVICE_ID) {
283 SensorDevice& dev(SensorDevice::getInstance());
284 return dev.configureDirectChannel(handle, getHalChannelHandle(), config);
285 } else {
286 return mService->configureRuntimeSensorDirectChannel(handle, this, config);
287 }
288}
289
Peng Xue36e3472016-11-03 11:57:10 -0700290void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700291 Mutex::Autolock _l(mConnectionLock);
292 stopAllLocked(backupRecord);
293}
Peng Xue36e3472016-11-03 11:57:10 -0700294
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700295void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) {
Peng Xue36e3472016-11-03 11:57:10 -0700296 struct sensors_direct_cfg_t config = {
297 .rate_level = SENSOR_DIRECT_RATE_STOP
298 };
299
Peng Xue36e3472016-11-03 11:57:10 -0700300 for (auto &i : mActivated) {
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100301 configure(i.first, &config);
Peng Xue36e3472016-11-03 11:57:10 -0700302 }
303
304 if (backupRecord && mActivatedBackup.empty()) {
305 mActivatedBackup = mActivated;
306 }
307 mActivated.clear();
308}
309
310void SensorService::SensorDirectConnection::recoverAll() {
Peng Xue36e3472016-11-03 11:57:10 -0700311 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700312 if (!mActivatedBackup.empty()) {
313 stopAllLocked(false);
Peng Xue36e3472016-11-03 11:57:10 -0700314
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700315 // recover list of report from backup
316 ALOG_ASSERT(mActivated.empty(),
317 "mActivated must be empty if mActivatedBackup was non-empty");
318 mActivated = mActivatedBackup;
319 mActivatedBackup.clear();
320
321 // re-enable them
322 for (auto &i : mActivated) {
323 struct sensors_direct_cfg_t config = {
324 .rate_level = i.second
325 };
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100326 configure(i.first, &config);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700327 }
Peng Xue36e3472016-11-03 11:57:10 -0700328 }
329}
330
331int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
332 return mHalChannelHandle;
333}
334
335bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const {
336 bool ret = false;
337
338 if (mMem.type == mem->type) {
339 switch (mMem.type) {
340 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
Peng Xu36407732017-05-16 15:12:22 -0700341 // there is no known method to test if two ashmem fds are equivalent besides
342 // trivially comparing the fd values (ino number from fstat() are always the
343 // same, pointing to "/dev/ashmem").
344 int fd1 = mMem.handle->data[0];
345 int fd2 = mem->handle->data[0];
346 ret = (fd1 == fd2);
Peng Xuf149b402017-02-09 12:24:25 -0800347 break;
Peng Xue36e3472016-11-03 11:57:10 -0700348 }
349 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700350 // there is no known method to test if two gralloc handle are equivalent
351 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800352 break;
Peng Xue36e3472016-11-03 11:57:10 -0700353 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700354 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700355 ALOGE("Unexpected mem type %d", mMem.type);
356 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800357 break;
Peng Xue36e3472016-11-03 11:57:10 -0700358 }
359 }
360 return ret;
361}
362
363} // namespace android
364