1 /*
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"
19 #include <android/util/ProtoOutputStream.h>
20 #include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
21 #include <hardware/sensors.h>
22 
23 #define UNUSED(x) (void)(x)
24 
25 namespace android {
26 
27 using util::ProtoOutputStream;
28 
SensorDirectConnection(const sp<SensorService> & service,uid_t uid,const sensors_direct_mem_t * mem,int32_t halChannelHandle,const String16 & opPackageName)29 SensorService::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),
34         mOpPackageName(opPackageName), mDestroyed(false) {
35     ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection");
36 }
37 
~SensorDirectConnection()38 SensorService::SensorDirectConnection::~SensorDirectConnection() {
39     ALOGD_IF(DEBUG_CONNECTIONS, "~SensorDirectConnection %p", this);
40     destroy();
41 }
42 
destroy()43 void SensorService::SensorDirectConnection::destroy() {
44     Mutex::Autolock _l(mDestroyLock);
45     // destroy once only
46     if (mDestroyed) {
47         return;
48     }
49 
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     }
56     mDestroyed = true;
57 }
58 
onFirstRef()59 void SensorService::SensorDirectConnection::onFirstRef() {
60 }
61 
dump(String8 & result) const62 void 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 
71 /**
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  */
dump(ProtoOutputStream * proto) const78 void 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 
getSensorChannel() const92 sp<BitTube> SensorService::SensorDirectConnection::getSensorChannel() const {
93     return nullptr;
94 }
95 
onSensorAccessChanged(bool hasAccess)96 void SensorService::SensorDirectConnection::onSensorAccessChanged(bool hasAccess) {
97     if (!hasAccess) {
98         stopAll(true /* backupRecord */);
99     } else {
100         recoverAll();
101     }
102 }
103 
hasSensorAccess() const104 bool SensorService::SensorDirectConnection::hasSensorAccess() const {
105     return mService->hasSensorAccess(mUid, mOpPackageName);
106 }
107 
enableDisable(int handle,bool enabled,nsecs_t samplingPeriodNs,nsecs_t maxBatchReportLatencyNs,int reservedFlags)108 status_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 
setEventRate(int handle,nsecs_t samplingPeriodNs)120 status_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 
flush()128 status_t SensorService::SensorDirectConnection::flush() {
129     // SensorDirectConnection does not support flush
130     return INVALID_OPERATION;
131 }
132 
configureChannel(int handle,int rateLevel)133 int32_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 
140     if (!hasSensorAccess()) {
141         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 
stopAll(bool backupRecord)183 void SensorService::SensorDirectConnection::stopAll(bool backupRecord) {
184     Mutex::Autolock _l(mConnectionLock);
185     stopAllLocked(backupRecord);
186 }
187 
stopAllLocked(bool backupRecord)188 void SensorService::SensorDirectConnection::stopAllLocked(bool backupRecord) {
189     struct sensors_direct_cfg_t config = {
190         .rate_level = SENSOR_DIRECT_RATE_STOP
191     };
192 
193     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 
recoverAll()204 void SensorService::SensorDirectConnection::recoverAll() {
205     Mutex::Autolock _l(mConnectionLock);
206     if (!mActivatedBackup.empty()) {
207         stopAllLocked(false);
208 
209         SensorDevice& dev(SensorDevice::getInstance());
210 
211         // 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         }
224     }
225 }
226 
getHalChannelHandle() const227 int32_t SensorService::SensorDirectConnection::getHalChannelHandle() const {
228     return mHalChannelHandle;
229 }
230 
isEquivalent(const sensors_direct_mem_t * mem) const231 bool 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: {
237                 // 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);
243                 break;
244             }
245             case SENSOR_DIRECT_MEM_TYPE_GRALLOC:
246                 // there is no known method to test if two gralloc handle are equivalent
247                 ret = false;
248                 break;
249             default:
250                 // should never happen
251                 ALOGE("Unexpected mem type %d", mMem.type);
252                 ret = true;
253                 break;
254         }
255     }
256     return ret;
257 }
258 
259 } // namespace android
260 
261