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(String8 packageName = String8(""), int mode = 0);
63     bool isDataInjectionEnabled();
64     int createDirectChannel(size_t size, int channelType, const native_handle_t *channelData);
65     void destroyDirectChannel(int channelNativeHandle);
66     int configureDirectChannel(int channelNativeHandle, int sensorHandle, int rateLevel);
67     int setOperationParameter(int handle, int type, const Vector<float> &floats, const Vector<int32_t> &ints);
68 
69 private:
70     // DeathRecipient interface
71     void sensorManagerDied();
72     static status_t waitForSensorService(sp<ISensorServer> *server);
73 
74     explicit SensorManager(const String16& opPackageName);
75     status_t assertStateLocked();
76 
77 private:
78     static Mutex sLock;
79     static std::map<String16, SensorManager*> sPackageInstances;
80 
81     Mutex mLock;
82     sp<ISensorServer> mSensorServer;
83     Sensor const** mSensorList;
84     Vector<Sensor> mSensors;
85     sp<IBinder::DeathRecipient> mDeathObserver;
86     const String16 mOpPackageName;
87     std::unordered_map<int, sp<ISensorEventConnection>> mDirectConnection;
88     int32_t mDirectConnectionHandle;
89 };
90 
91 // ----------------------------------------------------------------------------
92 }; // namespace android
93 
94 #endif // ANDROID_GUI_SENSOR_MANAGER_H
95