blob: e4c33dafcb8d505dcfd17d210b55c7f1c149a700 [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 Ishiguroe3ed3d22020-04-13 10:29:44 -070096void SensorService::SensorDirectConnection::onSensorAccessChanged(bool hasAccess) {
97 if (!hasAccess) {
98 stopAll(true /* backupRecord */);
99 } else {
100 recoverAll();
101 }
102}
103
104bool SensorService::SensorDirectConnection::hasSensorAccess() const {
105 return mService->hasSensorAccess(mUid, mOpPackageName);
106}
107
Peng Xue36e3472016-11-03 11:57:10 -0700108status_t SensorService::SensorDirectConnection::enableDisable(
109 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
110 int reservedFlags) {
111 // SensorDirectConnection does not support enableDisable, parameters not used
112 UNUSED(handle);
113 UNUSED(enabled);
114 UNUSED(samplingPeriodNs);
115 UNUSED(maxBatchReportLatencyNs);
116 UNUSED(reservedFlags);
117 return INVALID_OPERATION;
118}
119
120status_t SensorService::SensorDirectConnection::setEventRate(
121 int handle, nsecs_t samplingPeriodNs) {
122 // SensorDirectConnection does not support setEventRate, parameters not used
123 UNUSED(handle);
124 UNUSED(samplingPeriodNs);
125 return INVALID_OPERATION;
126}
127
128status_t SensorService::SensorDirectConnection::flush() {
129 // SensorDirectConnection does not support flush
130 return INVALID_OPERATION;
131}
132
133int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int rateLevel) {
134
135 if (handle == -1 && rateLevel == SENSOR_DIRECT_RATE_STOP) {
136 stopAll();
137 return NO_ERROR;
138 }
139
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700140 if (!hasSensorAccess()) {
Peng Xue36e3472016-11-03 11:57:10 -0700141 return PERMISSION_DENIED;
142 }
143
144 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
145 if (si == nullptr) {
146 return NAME_NOT_FOUND;
147 }
148
149 const Sensor& s = si->getSensor();
150 if (!SensorService::canAccessSensor(s, "config direct channel", mOpPackageName)) {
151 return PERMISSION_DENIED;
152 }
153
154 if (s.getHighestDirectReportRateLevel() == 0
155 || rateLevel > s.getHighestDirectReportRateLevel()
156 || !s.isDirectChannelTypeSupported(mMem.type)) {
157 return INVALID_OPERATION;
158 }
159
160 struct sensors_direct_cfg_t config = {
161 .rate_level = rateLevel
162 };
163
164 Mutex::Autolock _l(mConnectionLock);
165 SensorDevice& dev(SensorDevice::getInstance());
166 int ret = dev.configureDirectChannel(handle, getHalChannelHandle(), &config);
167
168 if (rateLevel == SENSOR_DIRECT_RATE_STOP) {
169 if (ret == NO_ERROR) {
170 mActivated.erase(handle);
171 } else if (ret > 0) {
172 ret = UNKNOWN_ERROR;
173 }
174 } else {
175 if (ret > 0) {
176 mActivated[handle] = rateLevel;
177 }
178 }
179
180 return ret;
181}
182
183void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700184 Mutex::Autolock _l(mConnectionLock);
185 stopAllLocked(backupRecord);
186}
Peng Xue36e3472016-11-03 11:57:10 -0700187
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700188void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) {
Peng Xue36e3472016-11-03 11:57:10 -0700189 struct sensors_direct_cfg_t config = {
190 .rate_level = SENSOR_DIRECT_RATE_STOP
191 };
192
Peng Xue36e3472016-11-03 11:57:10 -0700193 SensorDevice& dev(SensorDevice::getInstance());
194 for (auto &i : mActivated) {
195 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
196 }
197
198 if (backupRecord && mActivatedBackup.empty()) {
199 mActivatedBackup = mActivated;
200 }
201 mActivated.clear();
202}
203
204void SensorService::SensorDirectConnection::recoverAll() {
Peng Xue36e3472016-11-03 11:57:10 -0700205 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700206 if (!mActivatedBackup.empty()) {
207 stopAllLocked(false);
Peng Xue36e3472016-11-03 11:57:10 -0700208
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700209 SensorDevice& dev(SensorDevice::getInstance());
Peng Xue36e3472016-11-03 11:57:10 -0700210
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -0700211 // recover list of report from backup
212 ALOG_ASSERT(mActivated.empty(),
213 "mActivated must be empty if mActivatedBackup was non-empty");
214 mActivated = mActivatedBackup;
215 mActivatedBackup.clear();
216
217 // re-enable them
218 for (auto &i : mActivated) {
219 struct sensors_direct_cfg_t config = {
220 .rate_level = i.second
221 };
222 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
223 }
Peng Xue36e3472016-11-03 11:57:10 -0700224 }
225}
226
227int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
228 return mHalChannelHandle;
229}
230
231bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const {
232 bool ret = false;
233
234 if (mMem.type == mem->type) {
235 switch (mMem.type) {
236 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
Peng Xu36407732017-05-16 15:12:22 -0700237 // there is no known method to test if two ashmem fds are equivalent besides
238 // trivially comparing the fd values (ino number from fstat() are always the
239 // same, pointing to "/dev/ashmem").
240 int fd1 = mMem.handle->data[0];
241 int fd2 = mem->handle->data[0];
242 ret = (fd1 == fd2);
Peng Xuf149b402017-02-09 12:24:25 -0800243 break;
Peng Xue36e3472016-11-03 11:57:10 -0700244 }
245 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700246 // there is no known method to test if two gralloc handle are equivalent
247 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800248 break;
Peng Xue36e3472016-11-03 11:57:10 -0700249 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700250 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700251 ALOGE("Unexpected mem type %d", mMem.type);
252 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800253 break;
Peng Xue36e3472016-11-03 11:57:10 -0700254 }
255 }
256 return ret;
257}
258
259} // namespace android
260