• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 A_SENSOR_MANAGER_H_
18 
19 #define A_SENSOR_MANAGER_H_
20 
21 #include <android-base/macros.h>
22 #include <android/frameworks/sensorservice/1.0/ISensorManager.h>
23 #include <android/sensor.h>
24 #include <utils/Mutex.h>
25 #include <utils/RefBase.h>
26 
27 struct ALooper;
28 
29 struct ASensorManager {
30     static ASensorManager *getInstance();
31 
32     ASensorManager();
33     android::status_t initCheck() const;
34 
35     // Returns error or number of sensors returned.
36     int getSensorList(ASensorList *list);
37 
38     ASensorRef getDefaultSensor(int type);
39     ASensorRef getDefaultSensorEx(int type, bool wakeup);
40 
41     ASensorEventQueue *createEventQueue(
42             ALooper *looper,
43             int ident,
44             ALooper_callbackFunc callback,
45             void *data);
46 
47     // This must not be called from inside ALooper_callbackFunc to avoid deadlocking inside of the
48     // ALooper.
49     void destroyEventQueue(ASensorEventQueue *queue);
50 
51 private:
52 
53     struct SensorDeathRecipient : public android::hardware::hidl_death_recipient
54     {
55         // hidl_death_recipient interface
56         virtual void serviceDied(uint64_t cookie,
57                 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) override;
58     };
59 
60     using ISensorManager = android::frameworks::sensorservice::V1_0::ISensorManager;
61     using SensorInfo = android::hardware::sensors::V1_0::SensorInfo;
62 
63     static ASensorManager *sInstance;
64     android::sp<SensorDeathRecipient> mDeathRecipient = nullptr;
65 
66     android::status_t mInitCheck;
67     android::sp<ISensorManager> mManager;
68 
69     mutable android::Mutex mLock;
70     android::hardware::hidl_vec<SensorInfo> mSensors;
71     std::unique_ptr<ASensorRef[]> mSensorList;
72 
73     DISALLOW_COPY_AND_ASSIGN(ASensorManager);
74 };
75 
76 #endif  // A_SENSOR_MANAGER_H_
77