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