SensorList.cpp 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#include "SensorList.h" 18 19#include <hardware/sensors.h> 20#include <utils/String8.h> 21 22namespace android { 23namespace SensorServiceUtil { 24 25const Sensor SensorList::mNonSensor = Sensor("unknown"); 26 27bool SensorList::add( 28 int handle, SensorInterface* si, bool isForDebug, bool isVirtual) { 29 std::lock_guard<std::mutex> lk(mLock); 30 if (handle == si->getSensor().getHandle() && 31 mUsedHandle.insert(handle).second) { 32 // will succeed as the mUsedHandle does not have this handle 33 mHandleMap.emplace(handle, Entry(si, isForDebug, isVirtual)); 34 return true; 35 } 36 // handle exist already or handle mismatch 37 return false; 38} 39 40bool SensorList::remove(int handle) { 41 std::lock_guard<std::mutex> lk(mLock); 42 auto entry = mHandleMap.find(handle); 43 if (entry != mHandleMap.end()) { 44 mHandleMap.erase(entry); 45 return true; 46 } 47 return false; 48} 49 50String8 SensorList::getName(int handle) const { 51 return getOne<String8>( 52 handle, [] (const Entry& e) -> String8 {return e.si->getSensor().getName();}, 53 mNonSensor.getName()); 54} 55 56sp<SensorInterface> SensorList::getInterface(int handle) const { 57 return getOne<sp<SensorInterface>>( 58 handle, [] (const Entry& e) -> sp<SensorInterface> {return e.si;}, nullptr); 59} 60 61 62bool SensorList::isNewHandle(int handle) const { 63 std::lock_guard<std::mutex> lk(mLock); 64 return mUsedHandle.find(handle) == mUsedHandle.end(); 65} 66 67const Vector<Sensor> SensorList::getUserSensors() const { 68 // lock in forEachEntry 69 Vector<Sensor> sensors; 70 forEachEntry( 71 [&sensors] (const Entry& e) -> bool { 72 if (!e.isForDebug && !e.si->getSensor().isDynamicSensor()) { 73 sensors.add(e.si->getSensor()); 74 } 75 return true; 76 }); 77 return sensors; 78} 79 80const Vector<Sensor> SensorList::getUserDebugSensors() const { 81 // lock in forEachEntry 82 Vector<Sensor> sensors; 83 forEachEntry( 84 [&sensors] (const Entry& e) -> bool { 85 if (!e.si->getSensor().isDynamicSensor()) { 86 sensors.add(e.si->getSensor()); 87 } 88 return true; 89 }); 90 return sensors; 91} 92 93const Vector<Sensor> SensorList::getDynamicSensors() const { 94 // lock in forEachEntry 95 Vector<Sensor> sensors; 96 forEachEntry( 97 [&sensors] (const Entry& e) -> bool { 98 if (!e.isForDebug && e.si->getSensor().isDynamicSensor()) { 99 sensors.add(e.si->getSensor()); 100 } 101 return true; 102 }); 103 return sensors; 104} 105 106const Vector<Sensor> SensorList::getVirtualSensors() const { 107 // lock in forEachEntry 108 Vector<Sensor> sensors; 109 forEachEntry( 110 [&sensors] (const Entry& e) -> bool { 111 if (e.isVirtual) { 112 sensors.add(e.si->getSensor()); 113 } 114 return true; 115 }); 116 return sensors; 117} 118 119std::string SensorList::dump() const { 120 String8 result; 121 122 result.append("Sensor List:\n"); 123 forEachSensor([&result] (const Sensor& s) -> bool { 124 result.appendFormat( 125 "%-15s| %-10s| version=%d |%-20s| 0x%08x | \"%s\" | type=%d |", 126 s.getName().string(), 127 s.getVendor().string(), 128 s.getVersion(), 129 s.getStringType().string(), 130 s.getHandle(), 131 s.getRequiredPermission().string(), 132 s.getType()); 133 134 const int reportingMode = s.getReportingMode(); 135 if (reportingMode == AREPORTING_MODE_CONTINUOUS) { 136 result.append(" continuous | "); 137 } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) { 138 result.append(" on-change | "); 139 } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) { 140 result.append(" one-shot | "); 141 } else if (reportingMode == AREPORTING_MODE_SPECIAL_TRIGGER) { 142 result.append(" special-trigger | "); 143 } else { 144 result.append(" unknown-mode | "); 145 } 146 147 if (s.getMaxDelay() > 0) { 148 result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay()); 149 } else { 150 result.appendFormat("maxDelay=%dus | ", s.getMaxDelay()); 151 } 152 153 if (s.getMinDelay() > 0) { 154 result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay()); 155 } else { 156 result.appendFormat("minDelay=%dus | ", s.getMinDelay()); 157 } 158 159 if (s.getFifoMaxEventCount() > 0) { 160 result.appendFormat("FifoMax=%d events | ", 161 s.getFifoMaxEventCount()); 162 } else { 163 result.append("no batching | "); 164 } 165 166 if (s.isWakeUpSensor()) { 167 result.appendFormat("wakeUp | "); 168 } else { 169 result.appendFormat("non-wakeUp | "); 170 } 171 172 result.append("\n"); 173 return true; 174 }); 175 return std::string(result.string()); 176} 177 178SensorList::~SensorList() { 179} 180 181} // namespace SensorServiceUtil 182} // namespace android 183 184