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