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 <aidl/android/frameworks/sensorservice/ISensorManager.h> 22 #include <android-base/macros.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 static void serviceDied(void* cookie); 52 53 private: 54 using ISensorManager = aidl::android::frameworks::sensorservice::ISensorManager; 55 using SensorInfo = aidl::android::hardware::sensors::SensorInfo; 56 57 static ASensorManager *sInstance; 58 ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 59 60 android::status_t mInitCheck; 61 std::shared_ptr<ISensorManager> mManager; 62 63 mutable android::Mutex mQueuesLock; 64 std::vector<std::shared_ptr<ASensorEventQueue>> mQueues; 65 66 mutable android::Mutex mLock; 67 std::vector<SensorInfo> mSensors; 68 std::unique_ptr<ASensorRef[]> mSensorList; 69 70 DISALLOW_COPY_AND_ASSIGN(ASensorManager); 71 }; 72 73 #endif // A_SENSOR_MANAGER_H_ 74