blob: 106efd663424c68434494de11839ec25272e4ef0 [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
96status_t SensorService::SensorDirectConnection::enableDisable(
97 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
98 int reservedFlags) {
99 // SensorDirectConnection does not support enableDisable, parameters not used
100 UNUSED(handle);
101 UNUSED(enabled);
102 UNUSED(samplingPeriodNs);
103 UNUSED(maxBatchReportLatencyNs);
104 UNUSED(reservedFlags);
105 return INVALID_OPERATION;
106}
107
108status_t SensorService::SensorDirectConnection::setEventRate(
109 int handle, nsecs_t samplingPeriodNs) {
110 // SensorDirectConnection does not support setEventRate, parameters not used
111 UNUSED(handle);
112 UNUSED(samplingPeriodNs);
113 return INVALID_OPERATION;
114}
115
116status_t SensorService::SensorDirectConnection::flush() {
117 // SensorDirectConnection does not support flush
118 return INVALID_OPERATION;
119}
120
121int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int rateLevel) {
122
123 if (handle == -1 && rateLevel == SENSOR_DIRECT_RATE_STOP) {
124 stopAll();
125 return NO_ERROR;
126 }
127
Brian Stack5180e462019-03-08 17:15:19 -0800128 if (!mService->isOperationPermitted(mOpPackageName)) {
Peng Xue36e3472016-11-03 11:57:10 -0700129 return PERMISSION_DENIED;
130 }
131
132 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
133 if (si == nullptr) {
134 return NAME_NOT_FOUND;
135 }
136
137 const Sensor& s = si->getSensor();
138 if (!SensorService::canAccessSensor(s, "config direct channel", mOpPackageName)) {
139 return PERMISSION_DENIED;
140 }
141
142 if (s.getHighestDirectReportRateLevel() == 0
143 || rateLevel > s.getHighestDirectReportRateLevel()
144 || !s.isDirectChannelTypeSupported(mMem.type)) {
145 return INVALID_OPERATION;
146 }
147
148 struct sensors_direct_cfg_t config = {
149 .rate_level = rateLevel
150 };
151
152 Mutex::Autolock _l(mConnectionLock);
153 SensorDevice& dev(SensorDevice::getInstance());
154 int ret = dev.configureDirectChannel(handle, getHalChannelHandle(), &config);
155
156 if (rateLevel == SENSOR_DIRECT_RATE_STOP) {
157 if (ret == NO_ERROR) {
158 mActivated.erase(handle);
159 } else if (ret > 0) {
160 ret = UNKNOWN_ERROR;
161 }
162 } else {
163 if (ret > 0) {
164 mActivated[handle] = rateLevel;
165 }
166 }
167
168 return ret;
169}
170
171void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
172
173 struct sensors_direct_cfg_t config = {
174 .rate_level = SENSOR_DIRECT_RATE_STOP
175 };
176
177 Mutex::Autolock _l(mConnectionLock);
178 SensorDevice& dev(SensorDevice::getInstance());
179 for (auto &i : mActivated) {
180 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
181 }
182
183 if (backupRecord && mActivatedBackup.empty()) {
184 mActivatedBackup = mActivated;
185 }
186 mActivated.clear();
187}
188
189void SensorService::SensorDirectConnection::recoverAll() {
190 stopAll(false);
191
192 Mutex::Autolock _l(mConnectionLock);
193 SensorDevice& dev(SensorDevice::getInstance());
194
195 // recover list of report from backup
196 mActivated = mActivatedBackup;
197 mActivatedBackup.clear();
198
199 // re-enable them
200 for (auto &i : mActivated) {
201 struct sensors_direct_cfg_t config = {
202 .rate_level = i.second
203 };
204 dev.configureDirectChannel(i.first, getHalChannelHandle(), &config);
205 }
206}
207
208int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
209 return mHalChannelHandle;
210}
211
212bool SensorService::SensorDirectConnection::isEquivalent(const sensors_direct_mem_t *mem) const {
213 bool ret = false;
214
215 if (mMem.type == mem->type) {
216 switch (mMem.type) {
217 case SENSOR_DIRECT_MEM_TYPE_ASHMEM: {
Peng Xu36407732017-05-16 15:12:22 -0700218 // there is no known method to test if two ashmem fds are equivalent besides
219 // trivially comparing the fd values (ino number from fstat() are always the
220 // same, pointing to "/dev/ashmem").
221 int fd1 = mMem.handle->data[0];
222 int fd2 = mem->handle->data[0];
223 ret = (fd1 == fd2);
Peng Xuf149b402017-02-09 12:24:25 -0800224 break;
Peng Xue36e3472016-11-03 11:57:10 -0700225 }
226 case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
Peng Xuf88e2b92017-04-10 15:52:58 -0700227 // there is no known method to test if two gralloc handle are equivalent
228 ret = false;
Peng Xuf149b402017-02-09 12:24:25 -0800229 break;
Peng Xue36e3472016-11-03 11:57:10 -0700230 default:
Peng Xuf88e2b92017-04-10 15:52:58 -0700231 // should never happen
Peng Xue36e3472016-11-03 11:57:10 -0700232 ALOGE("Unexpected mem type %d", mMem.type);
233 ret = true;
Peng Xuf149b402017-02-09 12:24:25 -0800234 break;
Peng Xue36e3472016-11-03 11:57:10 -0700235 }
236 }
237 return ret;
238}
239
240} // namespace android
241