blob: b3be72d9b674674e11562544627716e69a865176 [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
17#include "SensorDevice.h"
18#include "SensorDirectConnection.h"
Mike Ma24743862020-01-29 00:36:55 -080019#include <android/util/ProtoOutputStream.h>
20#include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
Peng Xue36e3472016-11-03 11:57:10 -070021#include <hardware/sensors.h>
22
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 Phamaf91a912021-02-10 14:10:53 +010035 mIsRateCappedBasedOnPermission = mService->isRateCappedBasedOnPermission(mOpPackageName);
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) {
54 native_handle_close(mMem.handle);
55 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
105bool SensorService::SensorDirectConnection::hasSensorAccess() const {
106 return mService->hasSensorAccess(mUid, mOpPackageName);
107}
108
Peng Xue36e3472016-11-03 11:57:10 -0700109status_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
121status_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
129status_t SensorService::SensorDirectConnection::flush() {
130 // SensorDirectConnection does not support flush
131 return INVALID_OPERATION;
132}
133
134int32_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 Ishiguroe3ed3d22020-04-13 10:29:44 -0700141 if (!hasSensorAccess()) {
Peng Xue36e3472016-11-03 11:57:10 -0700142 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 Phamaf91a912021-02-10 14:10:53 +0100161 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 Xue36e3472016-11-03 11:57:10 -0700168 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
191void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700192 Mutex::Autolock _l(mConnectionLock);
193 stopAllLocked(backupRecord);
194}
Peng Xue36e3472016-11-03 11:57:10 -0700195
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700196void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) {
Peng Xue36e3472016-11-03 11:57:10 -0700197 struct sensors_direct_cfg_t config = {
198 .rate_level = SENSOR_DIRECT_RATE_STOP
199 };
200
Peng Xue36e3472016-11-03 11:57:10 -0700201 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
212void SensorService::SensorDirectConnection::recoverAll() {
Peng Xue36e3472016-11-03 11:57:10 -0700213 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700214 if (!mActivatedBackup.empty()) {
215 stopAllLocked(false);
Peng Xue36e3472016-11-03 11:57:10 -0700216
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700217 SensorDevice& dev(SensorDevice::getInstance());
Peng Xue36e3472016-11-03 11:57:10 -0700218
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700219 // 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 Xue36e3472016-11-03 11:57:10 -0700232 }
233}
234
235int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
236 return mHalChannelHandle;
237}
238
239bool 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 Xu36407732017-05-16 15:12:22 -0700245 // 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 Xuf149b402017-02-09 12:24:25 -0800251 break;
Peng Xue36e3472016-11-03 11:57:10 -0700252 }
253 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700254 // there is no known method to test if two gralloc handle are equivalent
255 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800256 break;
Peng Xue36e3472016-11-03 11:57:10 -0700257 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700258 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700259 ALOGE("Unexpected mem type %d", mMem.type);
260 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800261 break;
Peng Xue36e3472016-11-03 11:57:10 -0700262 }
263 }
264 return ret;
265}
266
267} // namespace android
268