blob: 7b4a86c2158f1ef1dd3fbf0e8dc18d0b391f8322 [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
2 * Copyright (C) 2010 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
Mathias Agopiana7352c92010-07-14 23:41:37 -070017#define LOG_TAG "Sensors"
18
Mathias Agopian801ea092017-03-06 15:05:04 -080019#include <sensor/SensorManager.h>
20
Mathias Agopian589ce852010-07-13 22:21:56 -070021#include <stdint.h>
22#include <sys/types.h>
23
Peng Xue36e3472016-11-03 11:57:10 -070024#include <cutils/native_handle.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070025#include <utils/Errors.h>
26#include <utils/RefBase.h>
27#include <utils/Singleton.h>
28
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +020029#include <android/companion/virtualnative/IVirtualDeviceManagerNative.h>
30
Mathias Agopian1a2b83a2011-10-16 22:15:23 -070031#include <binder/IBinder.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090032#include <binder/IPermissionController.h>
Mathias Agopiana7352c92010-07-14 23:41:37 -070033#include <binder/IServiceManager.h>
34
Mathias Agopian801ea092017-03-06 15:05:04 -080035#include <sensor/ISensorServer.h>
36#include <sensor/ISensorEventConnection.h>
37#include <sensor/Sensor.h>
38#include <sensor/SensorEventQueue.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070039
Shai Barack9b534db2024-01-25 00:16:03 +000040#include <com_android_hardware_libsensor_flags.h>
Rocky Fang71df3972024-06-04 17:49:33 +000041namespace libsensor_flags = com::android::hardware::libsensor::flags;
Shai Barack9b534db2024-01-25 00:16:03 +000042
Mathias Agopian589ce852010-07-13 22:21:56 -070043// ----------------------------------------------------------------------------
44namespace android {
45// ----------------------------------------------------------------------------
46
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +020047namespace {
48
49using ::android::companion::virtualnative::IVirtualDeviceManagerNative;
50
51static constexpr int DEVICE_ID_DEFAULT = 0;
52
53// Returns the deviceId of the device where this uid is observed. If the uid is present on more than
54// one devices, return the default deviceId.
55int getDeviceIdForUid(uid_t uid) {
56 sp<IBinder> binder =
57 defaultServiceManager()->checkService(String16("virtualdevice_native"));
58 if (binder != nullptr) {
59 auto vdm = interface_cast<IVirtualDeviceManagerNative>(binder);
60 std::vector<int> deviceIds;
61 vdm->getDeviceIdsForUid(uid, &deviceIds);
62 // If the UID is associated with multiple virtual devices, use the default device's
63 // sensors as we cannot disambiguate here. This effectively means that the app has
64 // activities on different devices at the same time, so it must handle the device
65 // awareness by itself.
66 if (deviceIds.size() == 1) {
67 const int deviceId = deviceIds.at(0);
68 int devicePolicy = IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT;
69 vdm->getDevicePolicy(deviceId,
70 IVirtualDeviceManagerNative::POLICY_TYPE_SENSORS,
71 &devicePolicy);
72 if (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM) {
73 return deviceId;
74 }
75 }
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +020076 }
77 return DEVICE_ID_DEFAULT;
78}
79
Rocky Fang71df3972024-06-04 17:49:33 +000080bool findSensorNameInList(int32_t handle, const Vector<Sensor>& sensorList,
81 std::string* outString) {
82 for (auto& sensor : sensorList) {
83 if (sensor.getHandle() == handle) {
84 std::ostringstream oss;
85 oss << sensor.getStringType() << ":" << sensor.getName();
86 if (outString) {
Yi Kong9dce90f2024-08-14 07:06:52 +080087 *outString = oss.str();
Rocky Fang71df3972024-06-04 17:49:33 +000088 }
89 return true;
90 }
91 }
92 return false;
93}
94
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +020095} // namespace
96
Peng Xufe5476a2017-03-17 17:27:42 -070097Mutex SensorManager::sLock;
98std::map<String16, SensorManager*> SensorManager::sPackageInstances;
Aravind Akellae2806cb2015-07-29 18:03:48 -070099
100SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) {
Peng Xufe5476a2017-03-17 17:27:42 -0700101 waitForSensorService(nullptr);
102
Aravind Akellae2806cb2015-07-29 18:03:48 -0700103 Mutex::Autolock _l(sLock);
104 SensorManager* sensorManager;
Peng Xufe5476a2017-03-17 17:27:42 -0700105 auto iterator = sPackageInstances.find(packageName);
Aravind Akellae2806cb2015-07-29 18:03:48 -0700106
Vladimir Komsiyski71db5f82023-12-08 09:02:20 +0100107 const uid_t uid = IPCThreadState::self()->getCallingUid();
108 const int deviceId = getDeviceIdForUid(uid);
109
110 // Return the cached instance if the device association of the package has not changed.
Aravind Akellae2806cb2015-07-29 18:03:48 -0700111 if (iterator != sPackageInstances.end()) {
112 sensorManager = iterator->second;
Vladimir Komsiyski71db5f82023-12-08 09:02:20 +0100113 if (sensorManager->mDeviceId == deviceId) {
114 return *sensorManager;
Aravind Akellae2806cb2015-07-29 18:03:48 -0700115 }
Aravind Akellae2806cb2015-07-29 18:03:48 -0700116 }
117
Vladimir Komsiyski71db5f82023-12-08 09:02:20 +0100118 // It is possible that the calling code has no access to the package name.
119 // In this case we will get the packages for the calling UID and pick the
120 // first one for attributing the app op. This will work correctly for
121 // runtime permissions as for legacy apps we will toggle the app op for
122 // all packages in the UID. The caveat is that the operation may be attributed
123 // to the wrong package and stats based on app ops may be slightly off.
124 String16 opPackageName = packageName;
125 if (opPackageName.size() <= 0) {
126 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
127 if (binder != nullptr) {
128 Vector<String16> packages;
129 interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
130 if (!packages.isEmpty()) {
131 opPackageName = packages[0];
132 } else {
133 ALOGE("No packages for calling UID");
134 }
135 } else {
136 ALOGE("Cannot get permission service");
137 }
138 }
139
140 sensorManager = new SensorManager(opPackageName, deviceId);
141
142 // If we had no package name, we looked it up from the UID and the sensor
143 // manager instance we created should also be mapped to the empty package
144 // name, to avoid looking up the packages for a UID and get the same result.
145 if (packageName.size() <= 0) {
146 sPackageInstances.insert(std::make_pair(String16(), sensorManager));
147 }
148
149 // Stash the per package sensor manager.
150 sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
151
Aravind Akellae2806cb2015-07-29 18:03:48 -0700152 return *sensorManager;
153}
154
Anthony Stange9352ab12023-02-21 17:57:38 +0000155void SensorManager::removeInstanceForPackage(const String16& packageName) {
156 Mutex::Autolock _l(sLock);
157 auto iterator = sPackageInstances.find(packageName);
158 if (iterator != sPackageInstances.end()) {
159 SensorManager* sensorManager = iterator->second;
160 delete sensorManager;
161 sPackageInstances.erase(iterator);
162 }
163}
164
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +0200165SensorManager::SensorManager(const String16& opPackageName, int deviceId)
166 : mSensorList(nullptr), mOpPackageName(opPackageName), mDeviceId(deviceId),
167 mDirectConnectionHandle(1) {
Brian Duddie8bbd6f42019-06-06 16:43:41 -0700168 Mutex::Autolock _l(mLock);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700169 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -0700170}
171
Peng Xu2576cb62016-01-20 00:22:09 -0800172SensorManager::~SensorManager() {
Mathias Agopiana7352c92010-07-14 23:41:37 -0700173 free(mSensorList);
Erik Staatsd35a5742022-02-04 06:37:58 -0800174 free(mDynamicSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700175}
176
Peng Xufe5476a2017-03-17 17:27:42 -0700177status_t SensorManager::waitForSensorService(sp<ISensorServer> *server) {
178 // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ...
179 sp<ISensorServer> s;
180 const String16 name("sensorservice");
181 for (int i = 0; i < 60; i++) {
182 status_t err = getService(name, &s);
183 switch (err) {
184 case NAME_NOT_FOUND:
185 sleep(1);
186 continue;
187 case NO_ERROR:
188 if (server != nullptr) {
189 *server = s;
190 }
191 return NO_ERROR;
192 default:
193 return err;
194 }
195 }
196 return TIMED_OUT;
197}
198
Peng Xu2576cb62016-01-20 00:22:09 -0800199void SensorManager::sensorManagerDied() {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700200 Mutex::Autolock _l(mLock);
201 mSensorServer.clear();
202 free(mSensorList);
Yi Kongd5e079f2018-07-17 16:08:27 -0700203 mSensorList = nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700204 mSensors.clear();
Erik Staatsd35a5742022-02-04 06:37:58 -0800205 free(mDynamicSensorList);
206 mDynamicSensorList = nullptr;
207 mDynamicSensors.clear();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700208}
209
Peng Xu2576cb62016-01-20 00:22:09 -0800210status_t SensorManager::assertStateLocked() {
Shai Barack9b534db2024-01-25 00:16:03 +0000211#if COM_ANDROID_HARDWARE_LIBSENSOR_FLAGS(SENSORMANAGER_PING_BINDER)
212 if (mSensorServer == nullptr) {
213#else
Aravind Akella8f35ca92015-08-17 15:22:12 -0700214 bool initSensorManager = false;
Yi Kongd5e079f2018-07-17 16:08:27 -0700215 if (mSensorServer == nullptr) {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700216 initSensorManager = true;
217 } else {
218 // Ping binder to check if sensorservice is alive.
219 status_t err = IInterface::asBinder(mSensorServer)->pingBinder();
220 if (err != NO_ERROR) {
221 initSensorManager = true;
222 }
223 }
224 if (initSensorManager) {
Shai Barack9b534db2024-01-25 00:16:03 +0000225#endif
Peng Xufe5476a2017-03-17 17:27:42 -0700226 waitForSensorService(&mSensorServer);
227 LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL");
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700228
229 class DeathObserver : public IBinder::DeathRecipient {
Peng Xu2576cb62016-01-20 00:22:09 -0800230 SensorManager& mSensorManager;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700231 virtual void binderDied(const wp<IBinder>& who) {
Yi Kong82d7c632019-09-20 12:10:47 -0700232 ALOGW("sensorservice died [%p]", static_cast<void*>(who.unsafe_get()));
Peng Xu2576cb62016-01-20 00:22:09 -0800233 mSensorManager.sensorManagerDied();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700234 }
235 public:
Chih-Hung Hsieh68a593e2016-04-28 09:14:32 -0700236 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700237 };
238
239 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800240 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700241
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +0200242 if (mDeviceId == DEVICE_ID_DEFAULT) {
243 mSensors = mSensorServer->getSensorList(mOpPackageName);
244 } else {
245 mSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, mDeviceId);
246 }
247
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700248 size_t count = mSensors.size();
Devin Moore49600b12023-04-25 00:17:13 +0000249 // If count is 0, mSensorList will be non-null. This is old
250 // existing behavior and callers expect this.
Dan Stozad723bd72014-11-18 10:24:03 -0800251 mSensorList =
252 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Yi Kongd5e079f2018-07-17 16:08:27 -0700253 LOG_ALWAYS_FATAL_IF(mSensorList == nullptr, "mSensorList NULL");
Aravind Akella8f35ca92015-08-17 15:22:12 -0700254
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700255 for (size_t i=0 ; i<count ; i++) {
256 mSensorList[i] = mSensors.array() + i;
257 }
258 }
259
260 return NO_ERROR;
261}
262
Peng Xu2576cb62016-01-20 00:22:09 -0800263ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700264 Mutex::Autolock _l(mLock);
265 status_t err = assertStateLocked();
266 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800267 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700268 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700269 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800270 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700271}
272
Vladimir Komsiyski157e4e02023-12-07 18:52:18 +0100273ssize_t SensorManager::getDefaultDeviceSensorList(Vector<Sensor> & list) {
274 Mutex::Autolock _l(mLock);
275 status_t err = assertStateLocked();
276 if (err < 0) {
277 return static_cast<ssize_t>(err);
278 }
279
280 if (mDeviceId == DEVICE_ID_DEFAULT) {
281 list = mSensors;
282 } else {
283 list = mSensorServer->getSensorList(mOpPackageName);
284 }
285
286 return static_cast<ssize_t>(list.size());
287}
288
Peng Xu2576cb62016-01-20 00:22:09 -0800289ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) {
290 Mutex::Autolock _l(mLock);
291 status_t err = assertStateLocked();
292 if (err < 0) {
293 return static_cast<ssize_t>(err);
294 }
295
296 dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
297 size_t count = dynamicSensors.size();
298
299 return static_cast<ssize_t>(count);
300}
301
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200302ssize_t SensorManager::getRuntimeSensorList(int deviceId, Vector<Sensor>& runtimeSensors) {
303 Mutex::Autolock _l(mLock);
304 status_t err = assertStateLocked();
305 if (err < 0) {
306 return static_cast<ssize_t>(err);
307 }
308
309 runtimeSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, deviceId);
310 size_t count = runtimeSensors.size();
311
312 return static_cast<ssize_t>(count);
313}
314
Erik Staatsd35a5742022-02-04 06:37:58 -0800315ssize_t SensorManager::getDynamicSensorList(Sensor const* const** list) {
316 Mutex::Autolock _l(mLock);
317 status_t err = assertStateLocked();
318 if (err < 0) {
319 return static_cast<ssize_t>(err);
320 }
321
322 free(mDynamicSensorList);
323 mDynamicSensorList = nullptr;
324 mDynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
325 size_t dynamicCount = mDynamicSensors.size();
326 if (dynamicCount > 0) {
327 mDynamicSensorList = static_cast<Sensor const**>(
328 malloc(dynamicCount * sizeof(Sensor*)));
329 if (mDynamicSensorList == nullptr) {
330 ALOGE("Failed to allocate dynamic sensor list for %zu sensors.",
331 dynamicCount);
332 return static_cast<ssize_t>(NO_MEMORY);
333 }
334
335 for (size_t i = 0; i < dynamicCount; i++) {
336 mDynamicSensorList[i] = mDynamicSensors.array() + i;
337 }
338 }
339
340 *list = mDynamicSensorList;
341 return static_cast<ssize_t>(mDynamicSensors.size());
342}
343
Mathias Agopiana7352c92010-07-14 23:41:37 -0700344Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700345{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700346 Mutex::Autolock _l(mLock);
347 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700348 bool wakeUpSensor = false;
349 // For the following sensor types, return a wake-up sensor. These types are by default
350 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
351 // a non_wake-up version.
352 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
353 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
Nick Vaccaro5e7f79b2016-10-17 15:40:51 -0700354 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE ||
Nick Vaccaro2e990eb2017-01-12 21:13:58 -0800355 type == SENSOR_TYPE_WRIST_TILT_GESTURE ||
Anthony Stangefdb1fc82020-01-16 15:02:48 -0500356 type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT || type == SENSOR_TYPE_HINGE_ANGLE) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700357 wakeUpSensor = true;
358 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700359 // For now we just return the first sensor of that type we find.
360 // in the future it will make sense to let the SensorService make
361 // that decision.
362 for (size_t i=0 ; i<mSensors.size() ; i++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700363 if (mSensorList[i]->getType() == type &&
364 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700365 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700366 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700367 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700368 }
Yi Kongd5e079f2018-07-17 16:08:27 -0700369 return nullptr;
Mathias Agopian589ce852010-07-13 22:21:56 -0700370}
371
Rocky Fang71df3972024-06-04 17:49:33 +0000372std::optional<std::string_view> SensorManager::getSensorNameByHandle(int32_t handle) {
373 std::lock_guard<std::mutex> lock(mSensorHandleToNameMutex);
374 auto iterator = mSensorHandleToName.find(handle);
375 if (iterator != mSensorHandleToName.end()) {
376 return iterator->second;
377 }
378
379 std::string sensorName;
380 if (!findSensorNameInList(handle, mSensors, &sensorName) &&
381 !findSensorNameInList(handle, mDynamicSensors, &sensorName)) {
382 ALOGW("Cannot find sensor with handle %d", handle);
383 return std::nullopt;
384 }
385
386 mSensorHandleToName[handle] = std::move(sensorName);
387
388 return mSensorHandleToName[handle];
389}
390
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800391sp<SensorEventQueue> SensorManager::createEventQueue(
392 String8 packageName, int mode, String16 attributionTag) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700393 sp<SensorEventQueue> queue;
394
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700395 Mutex::Autolock _l(mLock);
396 while (assertStateLocked() == NO_ERROR) {
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800397 sp<ISensorEventConnection> connection = mSensorServer->createSensorEventConnection(
398 packageName, mode, mOpPackageName, attributionTag);
Yi Kongd5e079f2018-07-17 16:08:27 -0700399 if (connection == nullptr) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700400 // SensorService just died or the app doesn't have required permissions.
401 ALOGE("createEventQueue: connection is NULL.");
Yi Kongd5e079f2018-07-17 16:08:27 -0700402 return nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700403 }
Rocky Fangc4c5c892024-08-07 23:28:55 +0000404 queue = new SensorEventQueue(connection, *this, packageName);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700405 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700406 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700407 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700408}
409
Aravind Akella841a5922015-06-29 12:37:48 -0700410bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700411 Mutex::Autolock _l(mLock);
412 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700413 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700414 }
Aravind Akella841a5922015-06-29 12:37:48 -0700415 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700416}
417
Mark Wheatley8f285d92023-07-07 20:07:18 +0000418bool SensorManager::isReplayDataInjectionEnabled() {
419 Mutex::Autolock _l(mLock);
420 if (assertStateLocked() == NO_ERROR) {
421 return mSensorServer->isReplayDataInjectionEnabled();
422 }
423 return false;
424}
425
426bool SensorManager::isHalBypassReplayDataInjectionEnabled() {
427 Mutex::Autolock _l(mLock);
428 if (assertStateLocked() == NO_ERROR) {
429 return mSensorServer->isHalBypassReplayDataInjectionEnabled();
430 }
431 return false;
432}
433
Peng Xue36e3472016-11-03 11:57:10 -0700434int SensorManager::createDirectChannel(
435 size_t size, int channelType, const native_handle_t *resourceHandle) {
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100436 static constexpr int DEFAULT_DEVICE_ID = 0;
437 return createDirectChannel(DEFAULT_DEVICE_ID, size, channelType, resourceHandle);
438}
439
440int SensorManager::createDirectChannel(
441 int deviceId, size_t size, int channelType, const native_handle_t *resourceHandle) {
Peng Xue36e3472016-11-03 11:57:10 -0700442 Mutex::Autolock _l(mLock);
443 if (assertStateLocked() != NO_ERROR) {
444 return NO_INIT;
445 }
446
Peng Xud9c8a862017-03-05 01:48:11 -0800447 if (channelType != SENSOR_DIRECT_MEM_TYPE_ASHMEM
448 && channelType != SENSOR_DIRECT_MEM_TYPE_GRALLOC) {
449 ALOGE("Bad channel shared memory type %d", channelType);
450 return BAD_VALUE;
Peng Xue36e3472016-11-03 11:57:10 -0700451 }
Peng Xud9c8a862017-03-05 01:48:11 -0800452
453 sp<ISensorEventConnection> conn =
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100454 mSensorServer->createSensorDirectConnection(mOpPackageName, deviceId,
Peng Xud9c8a862017-03-05 01:48:11 -0800455 static_cast<uint32_t>(size),
456 static_cast<int32_t>(channelType),
457 SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle);
458 if (conn == nullptr) {
459 return NO_MEMORY;
460 }
461
462 int nativeHandle = mDirectConnectionHandle++;
463 mDirectConnection.emplace(nativeHandle, conn);
464 return nativeHandle;
Peng Xue36e3472016-11-03 11:57:10 -0700465}
466
467void SensorManager::destroyDirectChannel(int channelNativeHandle) {
468 Mutex::Autolock _l(mLock);
469 if (assertStateLocked() == NO_ERROR) {
470 mDirectConnection.erase(channelNativeHandle);
471 }
472}
473
474int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) {
475 Mutex::Autolock _l(mLock);
476 if (assertStateLocked() != NO_ERROR) {
477 return NO_INIT;
478 }
479
480 auto i = mDirectConnection.find(channelNativeHandle);
481 if (i == mDirectConnection.end()) {
482 ALOGE("Cannot find the handle in client direct connection table");
483 return BAD_VALUE;
484 }
485
486 int ret;
487 ret = i->second->configureChannel(sensorHandle, rateLevel);
488 ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d",
489 static_cast<int>(sensorHandle), static_cast<int>(rateLevel),
490 static_cast<int>(ret));
491 return ret;
492}
493
Peng Xudd5c5cb2017-03-16 17:39:43 -0700494int SensorManager::setOperationParameter(
Alexey Polyudov88711e82017-05-23 19:54:04 -0700495 int handle, int type,
496 const Vector<float> &floats, const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700497 Mutex::Autolock _l(mLock);
498 if (assertStateLocked() != NO_ERROR) {
499 return NO_INIT;
500 }
Alexey Polyudov88711e82017-05-23 19:54:04 -0700501 return mSensorServer->setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700502}
503
Mathias Agopian589ce852010-07-13 22:21:56 -0700504// ----------------------------------------------------------------------------
505}; // namespace android