SensorList.h revision 755c451c7861a029e26e5f16e319b629169e656d
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
50    // After a handle is removed, the object that SensorInterface * pointing to may get deleted if
51    // no more sp<> of the same object exist.
52    bool remove(int handle);
53
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
62    String8 getName(int handle) const;
63    sp<SensorInterface> getInterface(int handle) const;
64    bool isNewHandle(int handle) const;
65
66    // Iterate through Sensor in sensor list and perform operation f on each Sensor object.
67    //
68    // TF is a function with the signature:
69    //    bool f(const Sensor &);
70    // A return value of 'false' stops the iteration immediately.
71    //
72    // Note: in the function f, it is illegal to make calls to member functions of the same
73    // SensorList object on which forEachSensor is invoked.
74    template <typename TF>
75    void forEachSensor(const TF& f) const;
76
77    const Sensor& getNonSensor() const { return mNonSensor;}
78
79    // Dumpable interface
80    virtual std::string dump() const override;
81
82    virtual ~SensorList();
83private:
84    struct Entry {
85        sp<SensorInterface> si;
86        const bool isForDebug;
87        const bool isVirtual;
88        Entry(SensorInterface* si_, bool debug_, bool virtual_) :
89            si(si_), isForDebug(debug_), isVirtual(virtual_) {
90        }
91    };
92
93    const static Sensor mNonSensor; //.getName() == "unknown",
94
95    // Iterate through Entry in sensor list and perform operation f on each Entry.
96    //
97    // TF is a function with the signature:
98    //    bool f(const Entry &);
99    // A return value of 'false' stops the iteration over entries immediately.
100    //
101    // Note: in the function being passed in, it is illegal to make calls to member functions of the
102    // same SensorList object on which forEachSensor is invoked.
103    template <typename TF>
104    void forEachEntry(const TF& f) const;
105
106    template <typename T, typename TF>
107    T getOne(int handle, const TF& accessor, T def = T()) const;
108
109    mutable std::mutex mLock;
110    std::map<int, Entry> mHandleMap;
111    std::unordered_set<int> mUsedHandle;
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