xref: /aosp_15_r20/frameworks/native/libs/sensor/include/sensor/SensorManager.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 
17 #ifndef ANDROID_GUI_SENSOR_MANAGER_H
18 #define ANDROID_GUI_SENSOR_MANAGER_H
19 
20 #include <binder/IBinder.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <sensor/SensorEventQueue.h>
24 #include <stdint.h>
25 #include <sys/types.h>
26 #include <utils/Errors.h>
27 #include <utils/String8.h>
28 #include <utils/StrongPointer.h>
29 #include <utils/Vector.h>
30 
31 #include <map>
32 #include <string>
33 #include <unordered_map>
34 
35 // ----------------------------------------------------------------------------
36 // Concrete types for the NDK
37 struct ASensorManager { };
38 
39 struct native_handle;
40 typedef struct native_handle native_handle_t;
41 
42 // ----------------------------------------------------------------------------
43 namespace android {
44 // ----------------------------------------------------------------------------
45 
46 class ISensorServer;
47 class Sensor;
48 class SensorEventQueue;
49 // ----------------------------------------------------------------------------
50 
51 class SensorManager : public ASensorManager
52 {
53 public:
54     static SensorManager& getInstanceForPackage(const String16& packageName);
55     static void removeInstanceForPackage(const String16& packageName);
56     ~SensorManager();
57 
58     ssize_t getSensorList(Sensor const* const** list);
59     ssize_t getDefaultDeviceSensorList(Vector<Sensor> & list);
60     ssize_t getDynamicSensorList(Vector<Sensor>& list);
61     ssize_t getDynamicSensorList(Sensor const* const** list);
62     ssize_t getRuntimeSensorList(int deviceId, Vector<Sensor>& list);
63     Sensor const* getDefaultSensor(int type);
64     sp<SensorEventQueue> createEventQueue(
65         String8 packageName = String8(""), int mode = 0, String16 attributionTag = String16(""));
66     bool isDataInjectionEnabled();
67     std::optional<std::string_view> getSensorNameByHandle(int32_t handle);
68     bool isReplayDataInjectionEnabled();
69     bool isHalBypassReplayDataInjectionEnabled();
70     int createDirectChannel(size_t size, int channelType, const native_handle_t *channelData);
71     int createDirectChannel(
72         int deviceId, size_t size, int channelType, const native_handle_t *channelData);
73     void destroyDirectChannel(int channelNativeHandle);
74     int configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel);
75     int setOperationParameter(int handle, int type, const Vector<float> &floats, const Vector<int32_t> &ints);
76 
77 private:
78     // DeathRecipient interface
79     void sensorManagerDied();
80     static status_t waitForSensorService(sp<ISensorServer> *server);
81 
82     explicit SensorManager(const String16& opPackageName, int deviceId);
83     status_t assertStateLocked();
84 
85 private:
86     static Mutex sLock;
87     static std::map<String16, SensorManager*> sPackageInstances;
88 
89     Mutex mLock;
90     sp<ISensorServer> mSensorServer;
91     Sensor const** mSensorList;
92     Vector<Sensor> mSensors;
93     Sensor const** mDynamicSensorList = nullptr;
94     Vector<Sensor> mDynamicSensors;
95     sp<IBinder::DeathRecipient> mDeathObserver;
96     const String16 mOpPackageName;
97     const int mDeviceId;
98     std::unordered_map<int, sp<ISensorEventConnection>> mDirectConnection;
99 
100     std::mutex mSensorHandleToNameMutex;
101     std::unordered_map<int32_t, std::string> mSensorHandleToName;
102     int32_t mDirectConnectionHandle;
103 };
104 
105 // ----------------------------------------------------------------------------
106 }; // namespace android
107 
108 #endif // ANDROID_GUI_SENSOR_MANAGER_H
109