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