1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SensorList.h"
#include <android/util/ProtoOutputStream.h>
#include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
#include <hardware/sensors.h>
#include <utils/String8.h>
#include <cinttypes>
namespace android {
namespace SensorServiceUtil {
const Sensor SensorList::mNonSensor = Sensor("unknown");
bool SensorList::add(
int handle, SensorInterface* si, bool isForDebug, bool isVirtual) {
std::lock_guard<std::mutex> lk(mLock);
if (handle == si->getSensor().getHandle() &&
mUsedHandle.insert(handle).second) {
// will succeed as the mUsedHandle does not have this handle
mHandleMap.emplace(handle, Entry(si, isForDebug, isVirtual));
return true;
}
// handle exist already or handle mismatch
return false;
}
bool SensorList::remove(int handle) {
std::lock_guard<std::mutex> lk(mLock);
auto entry = mHandleMap.find(handle);
if (entry != mHandleMap.end()) {
mHandleMap.erase(entry);
return true;
}
return false;
}
String8 SensorList::getName(int handle) const {
return getOne<String8>(
handle, [] (const Entry& e) -> String8 {return e.si->getSensor().getName();},
mNonSensor.getName());
}
String8 SensorList::getStringType(int handle) const {
return getOne<String8>(
handle, [] (const Entry& e) -> String8 {return e.si->getSensor().getStringType();},
mNonSensor.getStringType());
}
sp<SensorInterface> SensorList::getInterface(int handle) const {
return getOne<sp<SensorInterface>>(
handle, [] (const Entry& e) -> sp<SensorInterface> {return e.si;}, nullptr);
}
bool SensorList::isNewHandle(int handle) const {
std::lock_guard<std::mutex> lk(mLock);
return mUsedHandle.find(handle) == mUsedHandle.end();
}
const Vector<Sensor> SensorList::getUserSensors() const {
// lock in forEachEntry
Vector<Sensor> sensors;
forEachEntry(
[&sensors] (const Entry& e) -> bool {
if (!e.isForDebug && !e.si->getSensor().isDynamicSensor()) {
sensors.add(e.si->getSensor());
}
return true;
});
return sensors;
}
const Vector<Sensor> SensorList::getUserDebugSensors() const {
// lock in forEachEntry
Vector<Sensor> sensors;
forEachEntry(
[&sensors] (const Entry& e) -> bool {
if (!e.si->getSensor().isDynamicSensor()) {
sensors.add(e.si->getSensor());
}
return true;
});
return sensors;
}
const Vector<Sensor> SensorList::getDynamicSensors() const {
// lock in forEachEntry
Vector<Sensor> sensors;
forEachEntry(
[&sensors] (const Entry& e) -> bool {
if (!e.isForDebug && e.si->getSensor().isDynamicSensor()) {
sensors.add(e.si->getSensor());
}
return true;
});
return sensors;
}
const Vector<Sensor> SensorList::getVirtualSensors() const {
// lock in forEachEntry
Vector<Sensor> sensors;
forEachEntry(
[&sensors] (const Entry& e) -> bool {
if (e.isVirtual) {
sensors.add(e.si->getSensor());
}
return true;
});
return sensors;
}
std::string SensorList::dump() const {
String8 result;
forEachSensor([&result] (const Sensor& s) -> bool {
result.appendFormat(
"%#010x) %-25s | %-15s | ver: %" PRId32 " | type: %20s(%" PRId32
") | perm: %s | flags: 0x%08x\n",
s.getHandle(),
s.getName().c_str(),
s.getVendor().c_str(),
s.getVersion(),
s.getStringType().c_str(),
s.getType(),
s.getRequiredPermission().size() ? s.getRequiredPermission().c_str() : "n/a",
static_cast<int>(s.getFlags()));
result.append("\t");
const int reportingMode = s.getReportingMode();
if (reportingMode == AREPORTING_MODE_CONTINUOUS) {
result.append("continuous | ");
} else if (reportingMode == AREPORTING_MODE_ON_CHANGE) {
result.append("on-change | ");
} else if (reportingMode == AREPORTING_MODE_ONE_SHOT) {
result.append("one-shot | ");
} else if (reportingMode == AREPORTING_MODE_SPECIAL_TRIGGER) {
result.append("special-trigger | ");
} else {
result.append("unknown-mode | ");
}
if (s.getMaxDelay() > 0) {
result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay());
} else {
result.appendFormat("maxDelay=%" PRId32 "us | ", s.getMaxDelay());
}
if (s.getMinDelay() > 0) {
result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay());
} else {
result.appendFormat("minDelay=%" PRId32 "us | ", s.getMinDelay());
}
if (s.getFifoMaxEventCount() > 0) {
result.appendFormat("FIFO (max,reserved) = (%" PRIu32 ", %" PRIu32 ") events | ",
s.getFifoMaxEventCount(),
s.getFifoReservedEventCount());
} else {
result.append("no batching | ");
}
if (s.isWakeUpSensor()) {
result.appendFormat("wakeUp | ");
} else {
result.appendFormat("non-wakeUp | ");
}
if (s.isDataInjectionSupported()) {
result.appendFormat("data-injection, ");
}
if (s.isDynamicSensor()) {
result.appendFormat("dynamic, ");
}
if (s.hasAdditionalInfo()) {
result.appendFormat("has-additional-info, ");
}
result.append("\n");
if (s.getHighestDirectReportRateLevel() > SENSOR_DIRECT_RATE_STOP) {
result.appendFormat("\thighest rate level = %d, support shared mem: ",
s.getHighestDirectReportRateLevel());
if (s.isDirectChannelTypeSupported(SENSOR_DIRECT_MEM_TYPE_ASHMEM)) {
result.append("ashmem, ");
}
if (s.isDirectChannelTypeSupported(SENSOR_DIRECT_MEM_TYPE_GRALLOC)) {
result.append("gralloc, ");
}
result.append("\n");
}
return true;
});
return std::string(result.c_str());
}
/**
* Dump debugging information as android.service.SensorListProto protobuf message using
* ProtoOutputStream.
*
* See proto definition and some notes about ProtoOutputStream in
* frameworks/base/core/proto/android/service/sensor_service.proto
*/
void SensorList::dump(util::ProtoOutputStream* proto) const {
using namespace service::SensorListProto;
using namespace service::SensorListProto::SensorProto;
forEachSensor([&proto] (const Sensor& s) -> bool {
const uint64_t token = proto->start(SENSORS);
proto->write(HANDLE, s.getHandle());
proto->write(NAME, std::string(s.getName().c_str()));
proto->write(VENDOR, std::string(s.getVendor().c_str()));
proto->write(VERSION, s.getVersion());
proto->write(STRING_TYPE, std::string(s.getStringType().c_str()));
proto->write(TYPE, s.getType());
proto->write(REQUIRED_PERMISSION, std::string(s.getRequiredPermission().size() ?
s.getRequiredPermission().c_str() : ""));
proto->write(FLAGS, int(s.getFlags()));
switch (s.getReportingMode()) {
case AREPORTING_MODE_CONTINUOUS:
proto->write(REPORTING_MODE, RM_CONTINUOUS);
break;
case AREPORTING_MODE_ON_CHANGE:
proto->write(REPORTING_MODE, RM_ON_CHANGE);
break;
case AREPORTING_MODE_ONE_SHOT:
proto->write(REPORTING_MODE, RM_ONE_SHOT);
break;
case AREPORTING_MODE_SPECIAL_TRIGGER:
proto->write(REPORTING_MODE, RM_SPECIAL_TRIGGER);
break;
default:
proto->write(REPORTING_MODE, RM_UNKNOWN);
}
proto->write(MAX_DELAY_US, s.getMaxDelay());
proto->write(MIN_DELAY_US, s.getMinDelay());
proto->write(FIFO_MAX_EVENT_COUNT, int(s.getFifoMaxEventCount()));
proto->write(FIFO_RESERVED_EVENT_COUNT, int(s.getFifoReservedEventCount()));
proto->write(IS_WAKEUP, s.isWakeUpSensor());
proto->write(DATA_INJECTION_SUPPORTED, s.isDataInjectionSupported());
proto->write(IS_DYNAMIC, s.isDynamicSensor());
proto->write(HAS_ADDITIONAL_INFO, s.hasAdditionalInfo());
proto->write(HIGHEST_RATE_LEVEL, s.getHighestDirectReportRateLevel());
proto->write(ASHMEM, s.isDirectChannelTypeSupported(SENSOR_DIRECT_MEM_TYPE_ASHMEM));
proto->write(GRALLOC, s.isDirectChannelTypeSupported(SENSOR_DIRECT_MEM_TYPE_GRALLOC));
proto->write(MIN_VALUE, s.getMinValue());
proto->write(MAX_VALUE, s.getMaxValue());
proto->write(RESOLUTION, s.getResolution());
proto->write(POWER_USAGE, s.getPowerUsage());
proto->end(token);
return true;
});
}
SensorList::~SensorList() {
}
} // namespace SensorServiceUtil
} // namespace android
|