SensorList.h revision 0cc8f809924706c7d683da30605f432635dd5bb6
1/* 2 * Copyright (C) 2016 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_SENSOR_LIST_H 18#define ANDROID_SENSOR_LIST_H 19 20#include "SensorInterface.h" 21 22#include <gui/Sensor.h> 23#include <utils/String8.h> 24#include <utils/Vector.h> 25 26#include <mutex> 27#include <map> 28#include <string> 29#include <unordered_set> 30#include <vector> 31 32namespace android { 33class SensorInterface; 34 35namespace SensorServiceUtil { 36 37class Dumpable { 38public: 39 virtual std::string dump() const; 40 virtual void setFormat(std::string ) {} 41 virtual ~Dumpable() {} 42}; 43 44class SensorList : public Dumpable { 45public: 46 // After SensorInterface * is added into SensorList, it can be assumed that SensorList own the 47 // object it pointed to and the object should not be released elsewhere. 48 bool add(int handle, SensorInterface* si, bool isForDebug = false, bool isVirtual = false); 49 bool remove(int handle); 50 51 inline bool hasAnySensor() const { return mHandleMap.size() > 0;} 52 53 //helper functions 54 const Vector<Sensor> getUserSensors() const; 55 const Vector<Sensor> getUserDebugSensors() const; 56 const Vector<Sensor> getDynamicSensors() const; 57 const Vector<Sensor> getVirtualSensors() const; 58 59 String8 getName(int handle) const; 60 const Sensor& get(int handle) const; 61 SensorInterface* getInterface(int handle) const; 62 bool isNewHandle(int handle) const; 63 64 // Iterate through Sensor in sensor list and perform operation f on each Sensor object. 65 // 66 // TF is a function with the signature: 67 // bool f(const Sensor &); 68 // A return value of 'false' stops the iteration immediately. 69 // 70 // Note: in the function f, it is illegal to make calls to member functions of the same 71 // SensorList object on which forEachSensor is invoked. 72 template <typename TF> 73 void forEachSensor(const TF& f) const; 74 75 const Sensor& getNonSensor() const { return mNonSensor;} 76 77 // Dumpable interface 78 virtual std::string dump() const override; 79 80 virtual ~SensorList(); 81private: 82 struct Entry { 83 //TODO: use sp<> here 84 SensorInterface * const si; 85 const bool isForDebug; 86 const bool isVirtual; 87 Entry(SensorInterface* si_, bool debug_, bool virtual_) : 88 si(si_), isForDebug(debug_), isVirtual(virtual_) { 89 } 90 }; 91 92 const static Sensor mNonSensor; //.getName() == "unknown", 93 94 // Iterate through Entry in sensor list and perform operation f on each Entry. 95 // 96 // TF is a function with the signature: 97 // bool f(const Entry &); 98 // A return value of 'false' stops the iteration over entries immediately. 99 // 100 // Note: in the function being passed in, it is illegal to make calls to member functions of the 101 // same SensorList object on which forEachSensor is invoked. 102 template <typename TF> 103 void forEachEntry(const TF& f) const; 104 105 template <typename T, typename TF> 106 T getOne(int handle, const TF& accessor, T def = T()) const; 107 108 mutable std::mutex mLock; 109 std::map<int, Entry> mHandleMap; 110 std::unordered_set<int> mUsedHandle; 111 std::vector<SensorInterface *> mRecycle; 112}; 113 114template <typename TF> 115void SensorList::forEachSensor(const TF& f) const { 116 // lock happens in forEachEntry 117 forEachEntry([&f] (const Entry& e) -> bool { return f(e.si->getSensor());}); 118} 119 120template <typename TF> 121void SensorList::forEachEntry(const TF& f) const { 122 std::lock_guard<std::mutex> lk(mLock); 123 124 for (auto&& i : mHandleMap) { 125 if (!f(i.second)){ 126 break; 127 } 128 } 129} 130 131template <typename T, typename TF> 132T SensorList::getOne(int handle, const TF& accessor, T def) const { 133 std::lock_guard<std::mutex> lk(mLock); 134 auto i = mHandleMap.find(handle); 135 if (i != mHandleMap.end()) { 136 return accessor(i->second); 137 } else { 138 return def; 139 } 140} 141 142} // namespace SensorServiceUtil 143} // namespace android 144 145#endif // ANDROID_SENSOR_LIST_H 146