10cc8f809924706c7d683da30605f432635dd5bb6Peng Xu/* 20cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * Copyright (C) 2016 The Android Open Source Project 30cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * 40cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * Licensed under the Apache License, Version 2.0 (the "License"); 50cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * you may not use this file except in compliance with the License. 60cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * You may obtain a copy of the License at 70cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * 80cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * http://www.apache.org/licenses/LICENSE-2.0 90cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * 100cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * Unless required by applicable law or agreed to in writing, software 110cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * distributed under the License is distributed on an "AS IS" BASIS, 120cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * See the License for the specific language governing permissions and 140cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * limitations under the License. 150cc8f809924706c7d683da30605f432635dd5bb6Peng Xu */ 160cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 176a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#ifndef ANDROID_SENSOR_SERVICE_UTIL_SENSOR_LIST_H 186a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#define ANDROID_SENSOR_SERVICE_UTIL_SENSOR_LIST_H 190cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 200cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include "SensorInterface.h" 216a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#include "SensorServiceUtils.h" 220cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 230cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <gui/Sensor.h> 240cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <utils/String8.h> 250cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <utils/Vector.h> 260cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 270cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <map> 286a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#include <mutex> 290cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <unordered_set> 300cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <vector> 310cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 320cc8f809924706c7d683da30605f432635dd5bb6Peng Xunamespace android { 330cc8f809924706c7d683da30605f432635dd5bb6Peng Xuclass SensorInterface; 340cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 350cc8f809924706c7d683da30605f432635dd5bb6Peng Xunamespace SensorServiceUtil { 360cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 370cc8f809924706c7d683da30605f432635dd5bb6Peng Xuclass SensorList : public Dumpable { 380cc8f809924706c7d683da30605f432635dd5bb6Peng Xupublic: 390cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // After SensorInterface * is added into SensorList, it can be assumed that SensorList own the 400cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // object it pointed to and the object should not be released elsewhere. 410cc8f809924706c7d683da30605f432635dd5bb6Peng Xu bool add(int handle, SensorInterface* si, bool isForDebug = false, bool isVirtual = false); 42755c451c7861a029e26e5f16e319b629169e656dPeng Xu 43755c451c7861a029e26e5f16e319b629169e656dPeng Xu // After a handle is removed, the object that SensorInterface * pointing to may get deleted if 44755c451c7861a029e26e5f16e319b629169e656dPeng Xu // no more sp<> of the same object exist. 450cc8f809924706c7d683da30605f432635dd5bb6Peng Xu bool remove(int handle); 460cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 470cc8f809924706c7d683da30605f432635dd5bb6Peng Xu inline bool hasAnySensor() const { return mHandleMap.size() > 0;} 480cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 490cc8f809924706c7d683da30605f432635dd5bb6Peng Xu //helper functions 500cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const Vector<Sensor> getUserSensors() const; 510cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const Vector<Sensor> getUserDebugSensors() const; 520cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const Vector<Sensor> getDynamicSensors() const; 530cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const Vector<Sensor> getVirtualSensors() const; 540cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 550cc8f809924706c7d683da30605f432635dd5bb6Peng Xu String8 getName(int handle) const; 56755c451c7861a029e26e5f16e319b629169e656dPeng Xu sp<SensorInterface> getInterface(int handle) const; 570cc8f809924706c7d683da30605f432635dd5bb6Peng Xu bool isNewHandle(int handle) const; 580cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 590cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // Iterate through Sensor in sensor list and perform operation f on each Sensor object. 600cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // 610cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // TF is a function with the signature: 620cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // bool f(const Sensor &); 630cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // A return value of 'false' stops the iteration immediately. 640cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // 650cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // Note: in the function f, it is illegal to make calls to member functions of the same 660cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // SensorList object on which forEachSensor is invoked. 670cc8f809924706c7d683da30605f432635dd5bb6Peng Xu template <typename TF> 680cc8f809924706c7d683da30605f432635dd5bb6Peng Xu void forEachSensor(const TF& f) const; 690cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 700cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const Sensor& getNonSensor() const { return mNonSensor;} 710cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 720cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // Dumpable interface 730cc8f809924706c7d683da30605f432635dd5bb6Peng Xu virtual std::string dump() const override; 740cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 750cc8f809924706c7d683da30605f432635dd5bb6Peng Xu virtual ~SensorList(); 760cc8f809924706c7d683da30605f432635dd5bb6Peng Xuprivate: 770cc8f809924706c7d683da30605f432635dd5bb6Peng Xu struct Entry { 78755c451c7861a029e26e5f16e319b629169e656dPeng Xu sp<SensorInterface> si; 790cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const bool isForDebug; 800cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const bool isVirtual; 810cc8f809924706c7d683da30605f432635dd5bb6Peng Xu Entry(SensorInterface* si_, bool debug_, bool virtual_) : 820cc8f809924706c7d683da30605f432635dd5bb6Peng Xu si(si_), isForDebug(debug_), isVirtual(virtual_) { 830cc8f809924706c7d683da30605f432635dd5bb6Peng Xu } 840cc8f809924706c7d683da30605f432635dd5bb6Peng Xu }; 850cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 860cc8f809924706c7d683da30605f432635dd5bb6Peng Xu const static Sensor mNonSensor; //.getName() == "unknown", 870cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 880cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // Iterate through Entry in sensor list and perform operation f on each Entry. 890cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // 900cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // TF is a function with the signature: 910cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // bool f(const Entry &); 920cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // A return value of 'false' stops the iteration over entries immediately. 930cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // 940cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // Note: in the function being passed in, it is illegal to make calls to member functions of the 950cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // same SensorList object on which forEachSensor is invoked. 960cc8f809924706c7d683da30605f432635dd5bb6Peng Xu template <typename TF> 970cc8f809924706c7d683da30605f432635dd5bb6Peng Xu void forEachEntry(const TF& f) const; 980cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 990cc8f809924706c7d683da30605f432635dd5bb6Peng Xu template <typename T, typename TF> 1000cc8f809924706c7d683da30605f432635dd5bb6Peng Xu T getOne(int handle, const TF& accessor, T def = T()) const; 1010cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1020cc8f809924706c7d683da30605f432635dd5bb6Peng Xu mutable std::mutex mLock; 1030cc8f809924706c7d683da30605f432635dd5bb6Peng Xu std::map<int, Entry> mHandleMap; 1040cc8f809924706c7d683da30605f432635dd5bb6Peng Xu std::unordered_set<int> mUsedHandle; 1050cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}; 1060cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1070cc8f809924706c7d683da30605f432635dd5bb6Peng Xutemplate <typename TF> 1080cc8f809924706c7d683da30605f432635dd5bb6Peng Xuvoid SensorList::forEachSensor(const TF& f) const { 1090cc8f809924706c7d683da30605f432635dd5bb6Peng Xu // lock happens in forEachEntry 1100cc8f809924706c7d683da30605f432635dd5bb6Peng Xu forEachEntry([&f] (const Entry& e) -> bool { return f(e.si->getSensor());}); 1110cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} 1120cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1130cc8f809924706c7d683da30605f432635dd5bb6Peng Xutemplate <typename TF> 1140cc8f809924706c7d683da30605f432635dd5bb6Peng Xuvoid SensorList::forEachEntry(const TF& f) const { 1150cc8f809924706c7d683da30605f432635dd5bb6Peng Xu std::lock_guard<std::mutex> lk(mLock); 1160cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1170cc8f809924706c7d683da30605f432635dd5bb6Peng Xu for (auto&& i : mHandleMap) { 1180cc8f809924706c7d683da30605f432635dd5bb6Peng Xu if (!f(i.second)){ 1190cc8f809924706c7d683da30605f432635dd5bb6Peng Xu break; 1200cc8f809924706c7d683da30605f432635dd5bb6Peng Xu } 1210cc8f809924706c7d683da30605f432635dd5bb6Peng Xu } 1220cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} 1230cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1240cc8f809924706c7d683da30605f432635dd5bb6Peng Xutemplate <typename T, typename TF> 1250cc8f809924706c7d683da30605f432635dd5bb6Peng XuT SensorList::getOne(int handle, const TF& accessor, T def) const { 1260cc8f809924706c7d683da30605f432635dd5bb6Peng Xu std::lock_guard<std::mutex> lk(mLock); 1270cc8f809924706c7d683da30605f432635dd5bb6Peng Xu auto i = mHandleMap.find(handle); 1280cc8f809924706c7d683da30605f432635dd5bb6Peng Xu if (i != mHandleMap.end()) { 1290cc8f809924706c7d683da30605f432635dd5bb6Peng Xu return accessor(i->second); 1300cc8f809924706c7d683da30605f432635dd5bb6Peng Xu } else { 1310cc8f809924706c7d683da30605f432635dd5bb6Peng Xu return def; 1320cc8f809924706c7d683da30605f432635dd5bb6Peng Xu } 1330cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} 1340cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1350cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} // namespace SensorServiceUtil 1360cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} // namespace android 1370cc8f809924706c7d683da30605f432635dd5bb6Peng Xu 1386a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#endif // ANDROID_SENSOR_SERVICE_UTIL_SENSOR_LIST_H 139