blob: 1622e7753e5a2606e85afe9256598427f5cc9aa9 [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) {
Peng Xue36e3472016-11-03 11:57:10 -070035 ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection");
36}
37
38SensorService::SensorDirectConnection::~SensorDirectConnection() {
39 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorDirectConnection %p", this);
Peng Xu8cbefd72017-07-10 16:41:08 -070040 destroy();
41}
42
43void SensorService::SensorDirectConnection::destroy() {
44 Mutex::Autolock _l(mDestroyLock);
45 // destroy once only
46 if (mDestroyed) {
47 return;
48 }
Peng Xue36e3472016-11-03 11:57:10 -070049
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 Xu8cbefd72017-07-10 16:41:08 -070056 mDestroyed = true;
Peng Xue36e3472016-11-03 11:57:10 -070057}
58
59void SensorService::SensorDirectConnection::onFirstRef() {
60}
61
62void 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 Ma24743862020-01-29 00:36:55 -080071/**
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 */
78void 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 Xue36e3472016-11-03 11:57:10 -070092sp<BitTube> SensorService::SensorDirectConnection::getSensorChannel() const {
93 return nullptr;
94}
95
Arthur Ishiguro14c96b12020-03-26 15:09:03 -070096void SensorService::SensorDirectConnection::updateSensorSubscriptions() {
97 if (!hasSensorAccess()) {
98 stopAll(true /* backupRecord */);
99 } else {
100 recoverAll();
101 }
102}
103
104void SensorService::SensorDirectConnection::setSensorAccess(bool hasAccess) {
105 mHasSensorAccess = hasAccess;
106 updateSensorSubscriptions();
107}
108
109bool SensorService::SensorDirectConnection::hasSensorAccess() const {
110 return mHasSensorAccess && !mService->mSensorPrivacyPolicy->isSensorPrivacyEnabled();
111}
112
Peng Xue36e3472016-11-03 11:57:10 -0700113status_t SensorService::SensorDirectConnection::enableDisable(
114 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
115 int reservedFlags) {
116 // SensorDirectConnection does not support enableDisable, parameters not used
117 UNUSED(handle);
118 UNUSED(enabled);
119 UNUSED(samplingPeriodNs);
120 UNUSED(maxBatchReportLatencyNs);
121 UNUSED(reservedFlags);
122 return INVALID_OPERATION;
123}
124
125status_t SensorService::SensorDirectConnection::setEventRate(
126 int handle, nsecs_t samplingPeriodNs) {
127 // SensorDirectConnection does not support setEventRate, parameters not used
128 UNUSED(handle);
129 UNUSED(samplingPeriodNs);
130 return INVALID_OPERATION;
131}
132
133status_t SensorService::SensorDirectConnection::flush() {
134 // SensorDirectConnection does not support flush
135 return INVALID_OPERATION;
136}
137
138int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int rateLevel) {
139
140 if (handle == -1 && rateLevel == SENSOR_DIRECT_RATE_STOP) {
141 stopAll();
142 return NO_ERROR;
143 }
144
Brian Stack5180e462019-03-08 17:15:19 -0800145 if (!mService->isOperationPermitted(mOpPackageName)) {
Peng Xue36e3472016-11-03 11:57:10 -0700146 return PERMISSION_DENIED;
147 }
148
149 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
150 if (si == nullptr) {
151 return NAME_NOT_FOUND;
152 }
153
154 const Sensor& s = si->getSensor();
155 if (!SensorService::canAccessSensor(s, "config direct channel", mOpPackageName)) {
156 return PERMISSION_DENIED;
157 }
158
159 if (s.getHighestDirectReportRateLevel() == 0
160 || rateLevel > s.getHighestDirectReportRateLevel()
161 || !s.isDirectChannelTypeSupported(mMem.type)) {
162 return INVALID_OPERATION;
163 }
164
165 struct sensors_direct_cfg_t config = {
166 .rate_level = rateLevel
167 };
168
169 Mutex::Autolock _l(mConnectionLock);
170 SensorDevice& dev(SensorDevice::getInstance());
171 int ret = dev.configureDirectChannel(handle, getHalChannelHandle(), &config);
172
173 if (rateLevel == SENSOR_DIRECT_RATE_STOP) {
174 if (ret == NO_ERROR) {
175 mActivated.erase(handle);
176 } else if (ret > 0) {
177 ret = UNKNOWN_ERROR;
178 }
179 } else {
180 if (ret > 0) {
181 mActivated[handle] = rateLevel;
182 }
183 }
184
185 return ret;
186}
187
188void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
189
190 struct sensors_direct_cfg_t config = {
191 .rate_level = SENSOR_DIRECT_RATE_STOP
192 };
193
194 Mutex::Autolock _l(mConnectionLock);
195 SensorDevice& dev(SensorDevice::getInstance());
196 for (auto &i : mActivated) {
197 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
198 }
199
200 if (backupRecord && mActivatedBackup.empty()) {
201 mActivatedBackup = mActivated;
202 }
203 mActivated.clear();
204}
205
206void SensorService::SensorDirectConnection::recoverAll() {
207 stopAll(false);
208
209 Mutex::Autolock _l(mConnectionLock);
210 SensorDevice& dev(SensorDevice::getInstance());
211
212 // recover list of report from backup
213 mActivated = mActivatedBackup;
214 mActivatedBackup.clear();
215
216 // re-enable them
217 for (auto &i : mActivated) {
218 struct sensors_direct_cfg_t config = {
219 .rate_level = i.second
220 };
221 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
222 }
223}
224
225int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
226 return mHalChannelHandle;
227}
228
229bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const {
230 bool ret = false;
231
232 if (mMem.type == mem->type) {
233 switch (mMem.type) {
234 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
Peng Xu36407732017-05-16 15:12:22 -0700235 // there is no known method to test if two ashmem fds are equivalent besides
236 // trivially comparing the fd values (ino number from fstat() are always the
237 // same, pointing to "/dev/ashmem").
238 int fd1 = mMem.handle->data[0];
239 int fd2 = mem->handle->data[0];
240 ret = (fd1 == fd2);
Peng Xuf149b402017-02-09 12:24:25 -0800241 break;
Peng Xue36e3472016-11-03 11:57:10 -0700242 }
243 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700244 // there is no known method to test if two gralloc handle are equivalent
245 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800246 break;
Peng Xue36e3472016-11-03 11:57:10 -0700247 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700248 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700249 ALOGE("Unexpected mem type %d", mMem.type);
250 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800251 break;
Peng Xue36e3472016-11-03 11:57:10 -0700252 }
253 }
254 return ret;
255}
256
257} // namespace android
258