blob: 3ca6f0f9041e51c8490b05353a3367077ce4170d [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 }
76 } else {
77 ALOGW("Cannot get virtualdevice_native service");
78 }
79 return DEVICE_ID_DEFAULT;
80}
81
Rocky Fang71df3972024-06-04 17:49:33 +000082bool findSensorNameInList(int32_t handle, const Vector<Sensor>& sensorList,
83 std::string* outString) {
84 for (auto& sensor : sensorList) {
85 if (sensor.getHandle() == handle) {
86 std::ostringstream oss;
87 oss << sensor.getStringType() << ":" << sensor.getName();
88 if (outString) {
89 *outString = std::move(oss.str());
90 }
91 return true;
92 }
93 }
94 return false;
95}
96
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +020097} // namespace
98
Peng Xufe5476a2017-03-17 17:27:42 -070099Mutex SensorManager::sLock;
100std::map<String16, SensorManager*> SensorManager::sPackageInstances;
Aravind Akellae2806cb2015-07-29 18:03:48 -0700101
102SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) {
Peng Xufe5476a2017-03-17 17:27:42 -0700103 waitForSensorService(nullptr);
104
Aravind Akellae2806cb2015-07-29 18:03:48 -0700105 Mutex::Autolock _l(sLock);
106 SensorManager* sensorManager;
Peng Xufe5476a2017-03-17 17:27:42 -0700107 auto iterator = sPackageInstances.find(packageName);
Aravind Akellae2806cb2015-07-29 18:03:48 -0700108
Vladimir Komsiyski71db5f82023-12-08 09:02:20 +0100109 const uid_t uid = IPCThreadState::self()->getCallingUid();
110 const int deviceId = getDeviceIdForUid(uid);
111
112 // Return the cached instance if the device association of the package has not changed.
Aravind Akellae2806cb2015-07-29 18:03:48 -0700113 if (iterator != sPackageInstances.end()) {
114 sensorManager = iterator->second;
Vladimir Komsiyski71db5f82023-12-08 09:02:20 +0100115 if (sensorManager->mDeviceId == deviceId) {
116 return *sensorManager;
Aravind Akellae2806cb2015-07-29 18:03:48 -0700117 }
Aravind Akellae2806cb2015-07-29 18:03:48 -0700118 }
119
Vladimir Komsiyski71db5f82023-12-08 09:02:20 +0100120 // It is possible that the calling code has no access to the package name.
121 // In this case we will get the packages for the calling UID and pick the
122 // first one for attributing the app op. This will work correctly for
123 // runtime permissions as for legacy apps we will toggle the app op for
124 // all packages in the UID. The caveat is that the operation may be attributed
125 // to the wrong package and stats based on app ops may be slightly off.
126 String16 opPackageName = packageName;
127 if (opPackageName.size() <= 0) {
128 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
129 if (binder != nullptr) {
130 Vector<String16> packages;
131 interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
132 if (!packages.isEmpty()) {
133 opPackageName = packages[0];
134 } else {
135 ALOGE("No packages for calling UID");
136 }
137 } else {
138 ALOGE("Cannot get permission service");
139 }
140 }
141
142 sensorManager = new SensorManager(opPackageName, deviceId);
143
144 // If we had no package name, we looked it up from the UID and the sensor
145 // manager instance we created should also be mapped to the empty package
146 // name, to avoid looking up the packages for a UID and get the same result.
147 if (packageName.size() <= 0) {
148 sPackageInstances.insert(std::make_pair(String16(), sensorManager));
149 }
150
151 // Stash the per package sensor manager.
152 sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
153
Aravind Akellae2806cb2015-07-29 18:03:48 -0700154 return *sensorManager;
155}
156
Anthony Stange9352ab12023-02-21 17:57:38 +0000157void SensorManager::removeInstanceForPackage(const String16& packageName) {
158 Mutex::Autolock _l(sLock);
159 auto iterator = sPackageInstances.find(packageName);
160 if (iterator != sPackageInstances.end()) {
161 SensorManager* sensorManager = iterator->second;
162 delete sensorManager;
163 sPackageInstances.erase(iterator);
164 }
165}
166
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +0200167SensorManager::SensorManager(const String16& opPackageName, int deviceId)
168 : mSensorList(nullptr), mOpPackageName(opPackageName), mDeviceId(deviceId),
169 mDirectConnectionHandle(1) {
Brian Duddie8bbd6f42019-06-06 16:43:41 -0700170 Mutex::Autolock _l(mLock);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700171 assertStateLocked();
Mathias Agopian589ce852010-07-13 22:21:56 -0700172}
173
Peng Xu2576cb62016-01-20 00:22:09 -0800174SensorManager::~SensorManager() {
Mathias Agopiana7352c92010-07-14 23:41:37 -0700175 free(mSensorList);
Erik Staatsd35a5742022-02-04 06:37:58 -0800176 free(mDynamicSensorList);
Mathias Agopian589ce852010-07-13 22:21:56 -0700177}
178
Peng Xufe5476a2017-03-17 17:27:42 -0700179status_t SensorManager::waitForSensorService(sp<ISensorServer> *server) {
180 // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ...
181 sp<ISensorServer> s;
182 const String16 name("sensorservice");
183 for (int i = 0; i < 60; i++) {
184 status_t err = getService(name, &s);
185 switch (err) {
186 case NAME_NOT_FOUND:
187 sleep(1);
188 continue;
189 case NO_ERROR:
190 if (server != nullptr) {
191 *server = s;
192 }
193 return NO_ERROR;
194 default:
195 return err;
196 }
197 }
198 return TIMED_OUT;
199}
200
Peng Xu2576cb62016-01-20 00:22:09 -0800201void SensorManager::sensorManagerDied() {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700202 Mutex::Autolock _l(mLock);
203 mSensorServer.clear();
204 free(mSensorList);
Yi Kongd5e079f2018-07-17 16:08:27 -0700205 mSensorList = nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700206 mSensors.clear();
Erik Staatsd35a5742022-02-04 06:37:58 -0800207 free(mDynamicSensorList);
208 mDynamicSensorList = nullptr;
209 mDynamicSensors.clear();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700210}
211
Peng Xu2576cb62016-01-20 00:22:09 -0800212status_t SensorManager::assertStateLocked() {
Shai Barack9b534db2024-01-25 00:16:03 +0000213#if COM_ANDROID_HARDWARE_LIBSENSOR_FLAGS(SENSORMANAGER_PING_BINDER)
214 if (mSensorServer == nullptr) {
215#else
Aravind Akella8f35ca92015-08-17 15:22:12 -0700216 bool initSensorManager = false;
Yi Kongd5e079f2018-07-17 16:08:27 -0700217 if (mSensorServer == nullptr) {
Aravind Akella8f35ca92015-08-17 15:22:12 -0700218 initSensorManager = true;
219 } else {
220 // Ping binder to check if sensorservice is alive.
221 status_t err = IInterface::asBinder(mSensorServer)->pingBinder();
222 if (err != NO_ERROR) {
223 initSensorManager = true;
224 }
225 }
226 if (initSensorManager) {
Shai Barack9b534db2024-01-25 00:16:03 +0000227#endif
Peng Xufe5476a2017-03-17 17:27:42 -0700228 waitForSensorService(&mSensorServer);
229 LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL");
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700230
231 class DeathObserver : public IBinder::DeathRecipient {
Peng Xu2576cb62016-01-20 00:22:09 -0800232 SensorManager& mSensorManager;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700233 virtual void binderDied(const wp<IBinder>& who) {
Yi Kong82d7c632019-09-20 12:10:47 -0700234 ALOGW("sensorservice died [%p]", static_cast<void*>(who.unsafe_get()));
Peng Xu2576cb62016-01-20 00:22:09 -0800235 mSensorManager.sensorManagerDied();
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700236 }
237 public:
Chih-Hung Hsieh68a593e2016-04-28 09:14:32 -0700238 explicit DeathObserver(SensorManager& mgr) : mSensorManager(mgr) { }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700239 };
240
241 mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800242 IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700243
Vladimir Komsiyskif1d48af2023-10-06 10:11:44 +0200244 if (mDeviceId == DEVICE_ID_DEFAULT) {
245 mSensors = mSensorServer->getSensorList(mOpPackageName);
246 } else {
247 mSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, mDeviceId);
248 }
249
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700250 size_t count = mSensors.size();
Devin Moore49600b12023-04-25 00:17:13 +0000251 // If count is 0, mSensorList will be non-null. This is old
252 // existing behavior and callers expect this.
Dan Stozad723bd72014-11-18 10:24:03 -0800253 mSensorList =
254 static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
Yi Kongd5e079f2018-07-17 16:08:27 -0700255 LOG_ALWAYS_FATAL_IF(mSensorList == nullptr, "mSensorList NULL");
Aravind Akella8f35ca92015-08-17 15:22:12 -0700256
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700257 for (size_t i=0 ; i<count ; i++) {
258 mSensorList[i] = mSensors.array() + i;
259 }
260 }
261
262 return NO_ERROR;
263}
264
Peng Xu2576cb62016-01-20 00:22:09 -0800265ssize_t SensorManager::getSensorList(Sensor const* const** list) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700266 Mutex::Autolock _l(mLock);
267 status_t err = assertStateLocked();
268 if (err < 0) {
Dan Stozad723bd72014-11-18 10:24:03 -0800269 return static_cast<ssize_t>(err);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700270 }
Mathias Agopian589ce852010-07-13 22:21:56 -0700271 *list = mSensorList;
Dan Stozad723bd72014-11-18 10:24:03 -0800272 return static_cast<ssize_t>(mSensors.size());
Mathias Agopian589ce852010-07-13 22:21:56 -0700273}
274
Vladimir Komsiyski157e4e02023-12-07 18:52:18 +0100275ssize_t SensorManager::getDefaultDeviceSensorList(Vector<Sensor> & list) {
276 Mutex::Autolock _l(mLock);
277 status_t err = assertStateLocked();
278 if (err < 0) {
279 return static_cast<ssize_t>(err);
280 }
281
282 if (mDeviceId == DEVICE_ID_DEFAULT) {
283 list = mSensors;
284 } else {
285 list = mSensorServer->getSensorList(mOpPackageName);
286 }
287
288 return static_cast<ssize_t>(list.size());
289}
290
Peng Xu2576cb62016-01-20 00:22:09 -0800291ssize_t SensorManager::getDynamicSensorList(Vector<Sensor> & dynamicSensors) {
292 Mutex::Autolock _l(mLock);
293 status_t err = assertStateLocked();
294 if (err < 0) {
295 return static_cast<ssize_t>(err);
296 }
297
298 dynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
299 size_t count = dynamicSensors.size();
300
301 return static_cast<ssize_t>(count);
302}
303
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200304ssize_t SensorManager::getRuntimeSensorList(int deviceId, Vector<Sensor>& runtimeSensors) {
305 Mutex::Autolock _l(mLock);
306 status_t err = assertStateLocked();
307 if (err < 0) {
308 return static_cast<ssize_t>(err);
309 }
310
311 runtimeSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, deviceId);
312 size_t count = runtimeSensors.size();
313
314 return static_cast<ssize_t>(count);
315}
316
Erik Staatsd35a5742022-02-04 06:37:58 -0800317ssize_t SensorManager::getDynamicSensorList(Sensor const* const** list) {
318 Mutex::Autolock _l(mLock);
319 status_t err = assertStateLocked();
320 if (err < 0) {
321 return static_cast<ssize_t>(err);
322 }
323
324 free(mDynamicSensorList);
325 mDynamicSensorList = nullptr;
326 mDynamicSensors = mSensorServer->getDynamicSensorList(mOpPackageName);
327 size_t dynamicCount = mDynamicSensors.size();
328 if (dynamicCount > 0) {
329 mDynamicSensorList = static_cast<Sensor const**>(
330 malloc(dynamicCount * sizeof(Sensor*)));
331 if (mDynamicSensorList == nullptr) {
332 ALOGE("Failed to allocate dynamic sensor list for %zu sensors.",
333 dynamicCount);
334 return static_cast<ssize_t>(NO_MEMORY);
335 }
336
337 for (size_t i = 0; i < dynamicCount; i++) {
338 mDynamicSensorList[i] = mDynamicSensors.array() + i;
339 }
340 }
341
342 *list = mDynamicSensorList;
343 return static_cast<ssize_t>(mDynamicSensors.size());
344}
345
Mathias Agopiana7352c92010-07-14 23:41:37 -0700346Sensor const* SensorManager::getDefaultSensor(int type)
Mathias Agopian589ce852010-07-13 22:21:56 -0700347{
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700348 Mutex::Autolock _l(mLock);
349 if (assertStateLocked() == NO_ERROR) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700350 bool wakeUpSensor = false;
351 // For the following sensor types, return a wake-up sensor. These types are by default
352 // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
353 // a non_wake-up version.
354 if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
355 type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
Nick Vaccaro5e7f79b2016-10-17 15:40:51 -0700356 type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE ||
Nick Vaccaro2e990eb2017-01-12 21:13:58 -0800357 type == SENSOR_TYPE_WRIST_TILT_GESTURE ||
Anthony Stangefdb1fc82020-01-16 15:02:48 -0500358 type == SENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT || type == SENSOR_TYPE_HINGE_ANGLE) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700359 wakeUpSensor = true;
360 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700361 // For now we just return the first sensor of that type we find.
362 // in the future it will make sense to let the SensorService make
363 // that decision.
364 for (size_t i=0 ; i<mSensors.size() ; i++) {
Aravind Akellab37ba392014-08-05 14:53:07 -0700365 if (mSensorList[i]->getType() == type &&
366 mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700367 return mSensorList[i];
Aravind Akellab37ba392014-08-05 14:53:07 -0700368 }
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700369 }
Mathias Agopiana7352c92010-07-14 23:41:37 -0700370 }
Yi Kongd5e079f2018-07-17 16:08:27 -0700371 return nullptr;
Mathias Agopian589ce852010-07-13 22:21:56 -0700372}
373
Rocky Fang71df3972024-06-04 17:49:33 +0000374std::optional<std::string_view> SensorManager::getSensorNameByHandle(int32_t handle) {
375 std::lock_guard<std::mutex> lock(mSensorHandleToNameMutex);
376 auto iterator = mSensorHandleToName.find(handle);
377 if (iterator != mSensorHandleToName.end()) {
378 return iterator->second;
379 }
380
381 std::string sensorName;
382 if (!findSensorNameInList(handle, mSensors, &sensorName) &&
383 !findSensorNameInList(handle, mDynamicSensors, &sensorName)) {
384 ALOGW("Cannot find sensor with handle %d", handle);
385 return std::nullopt;
386 }
387
388 mSensorHandleToName[handle] = std::move(sensorName);
389
390 return mSensorHandleToName[handle];
391}
392
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800393sp<SensorEventQueue> SensorManager::createEventQueue(
394 String8 packageName, int mode, String16 attributionTag) {
Mathias Agopianbe58de02011-10-16 00:38:30 -0700395 sp<SensorEventQueue> queue;
396
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700397 Mutex::Autolock _l(mLock);
398 while (assertStateLocked() == NO_ERROR) {
Arthur Ishiguro340882c2021-02-18 15:17:44 -0800399 sp<ISensorEventConnection> connection = mSensorServer->createSensorEventConnection(
400 packageName, mode, mOpPackageName, attributionTag);
Yi Kongd5e079f2018-07-17 16:08:27 -0700401 if (connection == nullptr) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700402 // SensorService just died or the app doesn't have required permissions.
403 ALOGE("createEventQueue: connection is NULL.");
Yi Kongd5e079f2018-07-17 16:08:27 -0700404 return nullptr;
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700405 }
Rocky Fang71df3972024-06-04 17:49:33 +0000406 queue = new SensorEventQueue(connection, *this);
Mathias Agopian1a2b83a2011-10-16 22:15:23 -0700407 break;
Mathias Agopianbe58de02011-10-16 00:38:30 -0700408 }
Mathias Agopianbe58de02011-10-16 00:38:30 -0700409 return queue;
Mathias Agopian589ce852010-07-13 22:21:56 -0700410}
411
Aravind Akella841a5922015-06-29 12:37:48 -0700412bool SensorManager::isDataInjectionEnabled() {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700413 Mutex::Autolock _l(mLock);
414 if (assertStateLocked() == NO_ERROR) {
Aravind Akella841a5922015-06-29 12:37:48 -0700415 return mSensorServer->isDataInjectionEnabled();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700416 }
Aravind Akella841a5922015-06-29 12:37:48 -0700417 return false;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700418}
419
Mark Wheatley8f285d92023-07-07 20:07:18 +0000420bool SensorManager::isReplayDataInjectionEnabled() {
421 Mutex::Autolock _l(mLock);
422 if (assertStateLocked() == NO_ERROR) {
423 return mSensorServer->isReplayDataInjectionEnabled();
424 }
425 return false;
426}
427
428bool SensorManager::isHalBypassReplayDataInjectionEnabled() {
429 Mutex::Autolock _l(mLock);
430 if (assertStateLocked() == NO_ERROR) {
431 return mSensorServer->isHalBypassReplayDataInjectionEnabled();
432 }
433 return false;
434}
435
Peng Xue36e3472016-11-03 11:57:10 -0700436int SensorManager::createDirectChannel(
437 size_t size, int channelType, const native_handle_t *resourceHandle) {
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100438 static constexpr int DEFAULT_DEVICE_ID = 0;
439 return createDirectChannel(DEFAULT_DEVICE_ID, size, channelType, resourceHandle);
440}
441
442int SensorManager::createDirectChannel(
443 int deviceId, size_t size, int channelType, const native_handle_t *resourceHandle) {
Peng Xue36e3472016-11-03 11:57:10 -0700444 Mutex::Autolock _l(mLock);
445 if (assertStateLocked() != NO_ERROR) {
446 return NO_INIT;
447 }
448
Peng Xud9c8a862017-03-05 01:48:11 -0800449 if (channelType != SENSOR_DIRECT_MEM_TYPE_ASHMEM
450 && channelType != SENSOR_DIRECT_MEM_TYPE_GRALLOC) {
451 ALOGE("Bad channel shared memory type %d", channelType);
452 return BAD_VALUE;
Peng Xue36e3472016-11-03 11:57:10 -0700453 }
Peng Xud9c8a862017-03-05 01:48:11 -0800454
455 sp<ISensorEventConnection> conn =
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100456 mSensorServer->createSensorDirectConnection(mOpPackageName, deviceId,
Peng Xud9c8a862017-03-05 01:48:11 -0800457 static_cast<uint32_t>(size),
458 static_cast<int32_t>(channelType),
459 SENSOR_DIRECT_FMT_SENSORS_EVENT, resourceHandle);
460 if (conn == nullptr) {
461 return NO_MEMORY;
462 }
463
464 int nativeHandle = mDirectConnectionHandle++;
465 mDirectConnection.emplace(nativeHandle, conn);
466 return nativeHandle;
Peng Xue36e3472016-11-03 11:57:10 -0700467}
468
469void SensorManager::destroyDirectChannel(int channelNativeHandle) {
470 Mutex::Autolock _l(mLock);
471 if (assertStateLocked() == NO_ERROR) {
472 mDirectConnection.erase(channelNativeHandle);
473 }
474}
475
476int SensorManager::configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel) {
477 Mutex::Autolock _l(mLock);
478 if (assertStateLocked() != NO_ERROR) {
479 return NO_INIT;
480 }
481
482 auto i = mDirectConnection.find(channelNativeHandle);
483 if (i == mDirectConnection.end()) {
484 ALOGE("Cannot find the handle in client direct connection table");
485 return BAD_VALUE;
486 }
487
488 int ret;
489 ret = i->second->configureChannel(sensorHandle, rateLevel);
490 ALOGE_IF(ret < 0, "SensorManager::configureChannel (%d, %d) returns %d",
491 static_cast<int>(sensorHandle), static_cast<int>(rateLevel),
492 static_cast<int>(ret));
493 return ret;
494}
495
Peng Xudd5c5cb2017-03-16 17:39:43 -0700496int SensorManager::setOperationParameter(
Alexey Polyudov88711e82017-05-23 19:54:04 -0700497 int handle, int type,
498 const Vector<float> &floats, const Vector<int32_t> &ints) {
Peng Xudd5c5cb2017-03-16 17:39:43 -0700499 Mutex::Autolock _l(mLock);
500 if (assertStateLocked() != NO_ERROR) {
501 return NO_INIT;
502 }
Alexey Polyudov88711e82017-05-23 19:54:04 -0700503 return mSensorServer->setOperationParameter(handle, type, floats, ints);
Peng Xudd5c5cb2017-03-16 17:39:43 -0700504}
505
Mathias Agopian589ce852010-07-13 22:21:56 -0700506// ----------------------------------------------------------------------------
507}; // namespace android